In the recent Ajax approach to jquery, the data that needs to be returned is JSON data, and data processing in the success return differs in different ways to generate the JSON data, depending on how it is returned. In the $.ajax method should be how to deal with, simple to explain.
First gives the JSON data to be passed: [{"Demodata": "This is the JSON data"}]
1, using the normal ASPX page to handle
$.ajax ({
type: "Post",
URL: "default.aspx",
DataType: "JSON",
success:function (data) {
$ (" Input#showtime "). Val (data[0].demodata);
},
error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert (Errorthrown);
}
This is the code that passes the data in the background
Response.Clear ();
Response.Write ("[{\" demodata\ ": \" is the JSON Data\ "}]");
Response.Flush ();
This way of processing will pass over the data directly parsed into JSON data, that is, the foreground JS code here may be directly to parse the data into JSON object data, rather than string data, such as Data[0].demodata, here directly using this JSON object data
2, use WebService (ASMX) to process
This approach does not take the data passed over as a JSON object data, but rather as a string, as the following code
$.ajax ({
type: "Post",
URL: "Jquerycsmethodform.asmx/getdemodata",
dataType: "JSON", * * This sentence can not be used, no impact * *
contentType: "Application/json; Charset=utf-8 ",
success:function (data) {
$ (" Input#showtime "). Val (eval (' + DATA.D + ') ') [0].demodata);
There are two ways to convert data, the effect of two ways is the same as
//$ ("Input#showtime"). Val (eval (DATA.D) [0].demodata);
},
error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert (errorthrown);
}
Here is the method code for ASMX
public static string Getdemodata () {return
"[{\" demodata\ ": \" ' This is the JSON data\ '}] ';
}
The process here treats the JSON data passed back as a string, where the data is being processed in an eval to become the true JSON object data.
That
Success:function (data) {
eval (data);
The above is a small set to introduce the Ajax backstage success to the question of JSON data, I hope to help you, if you have any questions welcome to my message Small series will promptly reply to everyone!