Analysis of the json data transmitted by the Ajax backend success, ajaxjson
Recently, when using JQuery's ajax method, the data to be returned is json data. In success return, data processing uses different methods to generate json data based on different return methods. In the $. ajax method, how should we handle it.
First, provide The json Data to be transmitted: [{"demoData": "This Is The JSON Data"}]
1. Use a normal aspx page for processing
$.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 for passing data in the background
Response.Clear(); Response.Write("[{\"demoData\":\"This Is The JSON Data\"}]"); Response.Flush(); Response.End();
This processing method directly parses the transmitted data into json data. That is to say, the front-end js code here may directly parse the data into json object data instead of string data, for example, data [0]. demoData: The json object data is directly used here.
2. Use webservice (asmx) for processing
This method does not treat the transmitted data as json object data, but as a string for processing. The following code:
$. Ajax ({type: "post", url: "JqueryCSMethodForm. asmx/GetDemoData ", dataType:" json ",/* This sentence is usable and does not affect */contentType:" application/json; charset = UTF-8 ", success: function (data) {$ ("input # showTime "). val (eval ('+ data. d + ') [0]. demoData); // There are two data conversion methods. The two processing methods have the same effect. // $ ("input # showTime "). val (eval (data. d) [0]. demoData) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown );}});
The following is the asmx method code.
public static string GetDemoData() { return "[{\"demoData\":\"This Is The JSON Data\"}]"; }
This processing method treats the transmitted json data as a string, and the data must be processed eval so that it can become a real json object data.
That is
success:function(data){ eval(data); }
The above is a question about json data from the Ajax background success introduced by the editor. I hope it will help you. If you have any questions, please leave a message and I will reply to you in time!