• Ajax请求ashx返回json数据的常见问题(转自:http://blog.163.com/m13864039250_1/blog/static/21386524820133254431945/)


    1。请求text数据,在success事件中手动解析

    前台:
                    $.ajax({
                        type: "post",
                        url: "checkFile.ashx",
                        data: { "filename": "2" },
                        dataType: "text",
                        success: function (data) {
                            var json = eval('(' + data + ')');
                            alert(json.Age);
                        }
                    });
    后台处理:
                HttpResponse res = context.Response;
                HttpRequest req = context.Request;
                string code = req["filename"];
                string jsonString = "{"Age":28,"Name":"张三"}";
                //string jsonString = "{'Age':23,'Name':'abc'}";
                if (code != null)
                {
                    res.Write(jsonString);
                    res.ContentType = "text/plain";
                    res.End();
                }
    在这种情况下,单引号分割和转移双引号分割,都可以。
     
     
     
     
     
    2.请求json格式数据,jquery自动解析
    前台:
                    $.ajax({
                        type: "post",
                        url: "checkFile.ashx",
                        data: { "filename": "3" },
                       // contentType:"application/json",----------在ajax请求ashx文件的json数据时,此属性不能被指定,而在请求webservices时,是必须指定的。
                        dataType: "json",
                        success: function (data) {
                            alert(data.Name);
                        }
                    });
     
    后台处理:
                HttpResponse res = context.Response;
                HttpRequest req = context.Request;
                string code = req["filename"];
               string jsonString = "{"Age":28,"Name":"张三"}";
                if (code != null)
                {
                    res.Write(jsonString);
                    res.ContentType = "text/plain";
                    res.End();
                }
    在这种情况下,只有转移双引号分割,才可以在前台被jquery方法自动解析。
     
     
    3.带序列化的text数据前台解析
    前台:
                    $.ajax({
                        type: "post",
                        url: "checkFile.ashx",
                        data: { "filename": "2" },
                        dataType: "text",
                        success: function (data) {
                            $("p").text(data);
                            var json = eval('(' + data + ')');
                            alert(json.Name);
                        }
                    });
    json数据内容:   {"Name":"zhangsan","Code":111,"Birthday":"/Date(649666800000)/"}
    后台处理:
     
                HttpResponse res = context.Response;
                HttpRequest req = context.Request;
                string code = req["filename"];
                Student stu = new Student { 
                Name="zhangsan",
                Code=111,
                Birthday=Convert.ToDateTime("1990-8-3")
                };
                JavaScriptSerializer serializer=new JavaScriptSerializer();
                string jsonString = serializer.Serialize(stu);
    json数据内容:  "{"Name":"zhangsan","Code":111,"Birthday":"\/Date(649666800000)\/"}"
                if (code != null)
                {
                    res.Write(jsonString);
                    res.ContentType = "text/plain";
                    res.End();
                }
        [Serializable]
        public class Student
        {
            public string Name { get; set; }
            public int Code { get; set; }
            public DateTime Birthday { get; set; }
        }
    4.带序列化的json 前台自动解析:
    前台:
                    $.ajax({
                        type: "post",
                        url: "checkFile.ashx",
                        data: { "filename": "3" },
                        dataType: "json",--------------只要指定此处就可以,后台处理同上。
                        success: function (data) {
                            alert(data.Name);
                        }
                    });
  • 相关阅读:
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_import语句的解释
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_系统模块(sys)
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_time模块、datetime模块和calendar模块
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_语音合成模块(win32con)和语音控制模块(win32con)
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_窗体控制模块(win32con、win32gui)
    Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_操作系统模块(os)和队列模块(collections)
    python抓取百度百科点赞数等动态数据
    python 最长公共子序列
    中文unicode范围及unicode编解码
    python爬取并计算成绩
  • 原文地址:https://www.cnblogs.com/ANLOG/p/4260031.html
Copyright © 2020-2023  润新知