Ajax请求ashx返回各类数据的常见处理方式

  1 1。请求text数据,在success事件中手动解析
  2 前台:
  3                 $.ajax({
  4                     type: "post",
  5                     url: "checkFile.ashx",
  6                     data: { "filename": "2" },
  7                     dataType: "text",
  8                     success: function (data) {
  9                         var json = eval('(' + data + ')');
 10                         alert(json.Age);
 11                     }
 12                 });
 13 后台处理:
 14             HttpResponse res = context.Response;
 15             HttpRequest req = context.Request;
 16             string code = req["filename"];
 17             string jsonString = "{\"Age\":28,\"Name\":\"张三\"}";
 18             //string jsonString = "{'Age':23,'Name':'abc'}";
 19             if (code != null)
 20             {
 21                 res.Write(jsonString);
 22                 res.ContentType = "text/plain";
 23                 res.End();
 24             }
 25 在这种情况下,单引号分割和转移双引号分割,都可以。
 26 
 27 
 28 
 29 
 30 
 31 2.请求json格式数据,jquery自动解析
 32 前台:
 33                 $.ajax({
 34                     type: "post",
 35                     url: "checkFile.ashx",
 36                     data: { "filename": "3" },
 37                    // contentType:"application/json",----------在ajax请求ashx文件的json数据时,此属性不能被指定,而在请求webservices时,是必须指定的。
 38                     dataType: "json",
 39                     success: function (data) {
 40                         alert(data.Name);
 41                     }
 42                 });
 43 
 44 后台处理:
 45             HttpResponse res = context.Response;
 46             HttpRequest req = context.Request;
 47             string code = req["filename"];
 48            string jsonString = "{\"Age\":28,\"Name\":\"张三\"}";
 49             if (code != null)
 50             {
 51                 res.Write(jsonString);
 52                 res.ContentType = "text/plain";
 53                 res.End();
 54             }
 55 在这种情况下,只有转移双引号分割,才可以在前台被jquery方法自动解析。
 56 
 57 
 58 3.带序列化的text数据前台解析
 59 前台:
 60                 $.ajax({
 61                     type: "post",
 62                     url: "checkFile.ashx",
 63                     data: { "filename": "2" },
 64                     dataType: "text",
 65                     success: function (data) {
 66                         $("p").text(data);
 67                         var json = eval('(' + data + ')');
 68                         alert(json.Name);
 69                     }
 70                 });
 71 json数据内容:   {"Name":"zhangsan","Code":111,"Birthday":"\/Date(649666800000)\/"}
 72 后台处理:
 73 
 74             HttpResponse res = context.Response;
 75             HttpRequest req = context.Request;
 76             string code = req["filename"];
 77             Student stu = new Student { 
 78             Name="zhangsan",
 79             Code=111,
 80             Birthday=Convert.ToDateTime("1990-8-3")
 81             };
 82             JavaScriptSerializer serializer=new JavaScriptSerializer();
 83             string jsonString = serializer.Serialize(stu);
 84 json数据内容:  "{\"Name\":\"zhangsan\",\"Code\":111,\"Birthday\":\"\\/Date(649666800000)\\/\"}"
 85             if (code != null)
 86             {
 87                 res.Write(jsonString);
 88                 res.ContentType = "text/plain";
 89                 res.End();
 90             }
 91     [Serializable]
 92     public class Student
 93     {
 94         public string Name { get; set; }
 95         public int Code { get; set; }
 96         public DateTime Birthday { get; set; }
 97     }
 98 
 99 4. 带序列化的json 前台自动解析:
100    前台:
101                 $.ajax({
102                     type: "post",
103                     url: "checkFile.ashx",
104                     data: { "filename": "3" },
105                     dataType: "json",--------------只要指定此处就可以,后台处理同上。
106                     success: function (data) {
107                         alert(data.Name);
108                     }
109                 });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值