zoukankan      html  css  js  c++  java
  • jquery发送ajax请求返回数据格式

     1 jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等。
     2 
     3 1.html格式的数据
     4 
     5 "<div class='comment'><h6> "+username+" :</h6><p class='para'> "+content+" </p></div>"
     6 
     7 服务器端返回数据格式是html片段,因此不需要经过处理就可以将新的html数据插入到主页面中,这种方法虽然简便,但是重用性不强。
     8 
     9 $.ajax({
    10 
    11    type:"POST",
    12 
    13    url:"Handler.ashx",
    14 
    15    dataType:html,
    16 
    17    data:{username:$("#name").val(),password:$("#pwd").val()},
    18 
    19    success:function(data){
    20 
    21      $("#result").html(data);
    22 
    23    }
    24 
    25 }
    26 
    27 2.XML格式的数据
    28 
    29 Response.Write("<?xml version=""1.0"" encoding=""utf-8""?>")
    30 Response.Write("<comments>")
    31 Response.Write("<comment username='"+username+"'>")
    32 Response.Write("<content>"+content+"</content>")
    33 Response.Write("</comment>")
    34 Response.Write("</comments>")
    35 
    36 Response.End();
    37 
    38 由于服务器端返回的数据格式是XML文档,因此需要对文档的数据进行处理。
    39 
    40 $.ajax({
    41 
    42    type:"POST",
    43 
    44    url:"Handler.ashx",
    45 
    46    dataType:xml,
    47 
    48    data:{username:$("#name").val(),password:$("#pwd").val()},
    49 
    50    success:function(data){
    51 
    52      var username = $(data).find("comment").attr("username");
    53      var content = $(data).find("comment content").text();
    54      var txtHtml = "<div class='comment'><h6>"+      username+":</h6><p class='para'>"+content+"</p></div>";
    55      $("#result").html(txtHtml)
    56 
    57    }
    58 
    59 }
    60 
    61 3.JSON格式的数据
    62 
    63 Response.Write("{ username : '"+username+"' , content : '"+content+"'}") 
    64 
    65 由于服务器端返回的数据格式是JSON文档,因此也需要对文档的数据进行处理,但是JSON文档比较XML文档更容易解析。
    66 
    67 $.ajax({
    68 
    69    type:"POST",
    70 
    71    url:"Handler.ashx",
    72 
    73    dataType:json,
    74 
    75    data:{username:$("#name").val(),password:$("#pwd").val()},
    76 
    77    success:function(data){
    78 
    79      var username = data.username;
    80      var content = data.content;
    81      var txtHtml = "<div class='comment'><h6>"+      username+":</h6><p class='para'>"+content+"</p></div>";
    82      $("#result").html(txtHtml)
    83 
    84    }
    85 
    86 }
    87 
    88  
  • 相关阅读:
    WEBSERVICE 分析器错误信息: 未能创建类型
    Powerdesigner中表导出sql语句关于字段注释乱码的问题
    配置redis服务器允许远程连接
    [转]ubuntu系统重新分区、根目录扩容
    [转]自动驾驶平台Apollo 2.5环境搭建
    [转]在ROS下使用zeroconf配置多机通信
    ROS 安装完成后运行小乌龟示例程序
    [转]RoboWare Studio的使用和发布器/订阅器的编写与测试
    【转】ROS之topic和service通信比较
    【转】贝叶斯公式的直观理解(先验概率/后验概率)
  • 原文地址:https://www.cnblogs.com/kangshuai/p/4929301.html
Copyright © 2011-2022 走看看