Recently, when using the ajax method of jquery, the data returned is required to be json data. I encountered the following problems during processingThat is when using different methods to generate json data,How should it be handled in the $.ajax method,The following describes them in order,Since I use asp.net, the pages I process are all used.net to do it! The other way should be the same

First, some basic knowledge of json.

Objects in json are identified by "{}", an "{}" represents an object,Such as {"areaid":"123"}, the value of the object is in the form of key-value pairs (key:value).

"[]", Identifies the array,Each data in the array is separated by ",", such as ["areaid":"123", "areaid":"345"].

In many cases it is an array of objects,That's it:

[{"Areaid":"123"}, {"areaid":"345"}]

In fact, an array is also an object.The above format can also be written like this:

{"Area":​​[{"areaid":"123"}, {"areaid":"345"}]}

This represents an area object, which has two child data,Each child data is also an object,Each child object is an areaid.

The definition format of strings and characters in json is similar to the general C-like language definition.Double quotes define a string,Single quotes define characters.

The json key is enclosed in double quotes.For example, the above "area" and "areaid" are enclosed in double quotes.When constructing json strings in some languages,You can escape double quotes using escape characters.

First give the json data to be transmitted:[{"demodata":"this is the json data"}]

1.Use ordinary aspx pages to process

I think it is the easiest to handle this way.Look at the following 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 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 these data into json object data.
Not string data,Such as data [0] .demodata,</p>
Here I directly use this json object data
2, using webservice (asmx) to handle this processing method will not treat the passed data as json object data
</strong>but are treated as strings,The following code
$.ajax ({
type:"post",url:"jquerycsmethodform.asmx/getdemodata",datatype:"json",/* This sentence can be used or not,No effect*/
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 the two processing methods is the same //$("input#showtime").val(eval(data.d)[0].demodata);
},error:function (xmlhttprequest, textstatus, errorthrown) {
alert (errorthrown);

Here is the asmx method code [webmethod]

public static string getdemodata () {
return "[{\" demodata \ ":\" this is the json data \ "}]";

This processing method here treats the returned json data as a string.It is necessary to perform eval processing on this data, so that it can become real json object data.

3, the use of ashx files to handle this way is the same as ordinary aspx page processing, so there is no more explanation here!

  • Previous Java connection to six types of database skills Raiders
  • Next JS dynamically generates Html elements to implement Post operation (createElement)