1. Request text data for manual parsing in the success event
Front Desk: $.ajax ({type: "post", url: "Checkfile.ashx", Data: {"filename": "2"},
dataType: "Text",Success:function (data) {var json = eval (' (' + data + ') '); Alert (JSON. Age); }); background processing: HttpResponse res = context. Response; HttpRequest req = context. Request; String code = req["FileName"];string jsonstring = "{\" age\ ": 28,\" name\ ": \" Zhang San \ "}";//string jsonstring = "{' Age ':", ' Name ': ' abc '} ";if (code! = null) {Res. Write (jsonstring);
Res. ContentType = "Text/plain";Res. End (); }
in this case, both single-quote and transfer-double-quote splits are possible.
2. Request JSON format data, jquery automatic parsingFront Desk: $.ajax ({type: "post", url: "Checkfile.ashx", Data: {"filename": "3"},//ContentType: "Application/json",----------This property cannot be specified when AJAX requests JSON data for a ashx file, but must be specified when requesting webservices. DataType: "JSON", success:function (data) { &NBSP ; alert (data. Name); } &NB Sp ); background processing: HttpResponse res = context. response; HttpRequest req = context. request; String code = req["FileName"]; s Tring jsonstring = "{\" age\ ": 28,\" name\ ": \" Zhang San \ "}"; if (code! = null) { Res. Write (jsonstring); Res. ContentType = "TeXt/plain "; Res. End (); }
in this case, only the transfer of double quotation marks can be automatically parsed in the foreground by the jquery method.
3. Foreground parsing of text data with serializationFront desk: $.ajax ({ Type: "POST", URL: "Checkfile.ashx", &N Bsp data: {"filename": "2"}, &NBSP ; DataType: "text", Success:function (data) { $ ("P"). Text ( Data); var json = eval (' (' + data + ') ' ); alert (JSON. Name); } &NB Sp });
JSON data content: {"Name": "Zhangsan", "Code": 111, "Birthday": "\/date (649666800000) \"}Background processing: 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 data content: "{\" 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 with serialized foreground auto parse: Front desk: $.ajax ({type: "post", url: "Checkfile.ashx", Data: {"filename": "3"},dataType: "JSON",--------------as long as you specify here, background processing ibid. Success:function (data) {alert (data). Name); } });
Ajax, JSON some finishing (1)