Home  >  Article  >  Web Front-end  >  How to process json data uploaded by ajax background success

How to process json data uploaded by ajax background success

php中世界最好的语言
php中世界最好的语言Original
2018-04-03 14:05:431607browse

This time I will show you how to process the json data uploaded by ajax background success, and what are the precautions for handling json data uploaded by ajax background success. The following is a practical case, let's take a look.

When using JQuery's ajax method recently, the data that needs to be returned is json data. In the success return, data processing will use different methods to generate json data according to the return method. How to handle it in the $.ajax method is briefly explained.

First give the json data to be transmitted: [{"demoData":"This Is The JSON Data"}]

1, use an ordinary aspx page to process

$.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 passed data into json data, which means that the front-end js code here may directly parse the data into jsonObject data, not string data, such as data[0].demoData, this json object data is used directly here

2, use webservice (asmx) Processing

This processing method will not treat the passed data as json object data, but as a string. The following code

$.ajax({ 
type: "post", 
url: "JqueryCSMethodForm.asmx/GetDemoData", 
dataType: "json",/*这句可用可不用,没有影响*/ 
contentType: "application/json; charset=utf-8", 
success: function (data) { 
$("input#showTime").val(eval('(' + data.d + ')')[0].demoData); 
//这里有两种对数据的转换方式,两处理方式的效果一样 
//$("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 here treats the passed json data as a string, and the data must be eval processed so that it can become a real json object data.

That is

success:function(data){ 
eval(data); 
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How ajax handles the data type returned by the server

Ajax request WebService cross-domain implementation method ( Code attached)

The above is the detailed content of How to process json data uploaded by ajax background success. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn