When using the Ajax method of jquery recently, the data that needs to be returned is JSON data, and data processing in success returns different ways to generate JSON data depending on how it is returned. How it should be handled in the $.ajax method is simply explained.
First, give the JSON data to be transmitted: [{"Demodata": "This is the JSON"}]
1, use the normal ASPX page to handle
JS Code
- $.ajax ({
- Type: "POST",
- URL: "default.aspx",
- DataType: "JSON",
- Success: function (data) {
- $ ("Input#showtime"). Val (Data[0].demodata);
- },
- Error: function (XMLHttpRequest, textstatus, Errorthrown) {
- alert (Errorthrown);
- }
- });
Here is the code that passes the data in the background
Java Code
- Response.Clear ();
- Response.Write ("[{\" demodata\ ": \" This is the JSON data\ "}]");
- Response.Flush ();
- Response.End ();
This process of processing the data passed directly into the JSON data, that is to say, the foreground JS code can directly parse the data into JSON object data, rather than string data, such as Data[0].demodata, this is the direct use of this JSON object data
2, using WebService (ASMX) to process
This approach does not treat the passed data as JSON object data, but rather as a string, as in the following code
JS 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, and the effect of two processing methods is the same
- //$ ("Input#showtime"). Val (eval (DATA.D) [0].demodata];
- },
- Error: function (XMLHttpRequest, textstatus, Errorthrown) {
- alert (Errorthrown);
- }
- });
Here is the method code for ASMX
Java Code
- Public static String Getdemodata () {
- return "[{\" demodata\ ": \" This is the JSON data\ "}]";
- }
The processing here treats the JSON data that is passed back as a string, where the data is processed by eval to be the real JSON object data.
That
Java Code
- Success:function (data) {
- eval (data);
- }
Ajax gets JSON data passed in the background