Therefore, when parsing background data, we need to handle and handle it according to background data.
Here, I use the wcf Service provided by asp.net in the background, and there are also general ashx handlers. The general principle is similar.
Objects that we often use in C # include object objects such as User and List sets, which are generally returned lists.
Complex points include object nested objects or list sets. However, there is no difference. You only need to check the amount of your data to determine whether js processes the data,
Or directly return the final result after processing in the background.
1. Object: if an object is returned, the object data in js is the same as that in your background code class.
For example, the following code obtains an object. You can directly obtain it using its name attribute.
Copy codeThe Code is as follows:
$. Ajax ({
Type: "post ",
DataType: "json", traditional: true,
Data: {identifier: "edit", sid: id },
Url: AjaxUrl,
Success: function (data, textStatus ){
If (data! = Null ){
If (data ){
$ ("# Name"). val (data. Name); the object is obtained.
SetSelectOpertionValue ("selectRelation", data. Relation );
SetSelectOpertionValue ("selectaddreason", data. Reason );
} Else {
$ ("# BtnAdd"). attr ("disabled", false); $ ("# btnAdd"). text ("edit ");
}
}
},
Complete: function
(XMLHttpRequest, textStatus ){
},
Error: function
(E ){
$ ("# BtnAdd"). attr ("disabled", false); $ ("# btnAdd"). text ("edit ");
}
});
2. The returned data is a List set that contains some objects. There are also many application scenarios.
In js, it corresponds to an array. The array contains the object entity you returned. You can use each for traversal. For details, refer:
[Jquery js Array Operations and object examples]
Demo:
Copy codeThe Code is as follows:
$. Ajax ({type: "post ",
DataType: "json", traditional: true,
Data: {tags: "list", lc: ID, nm: $ ("# searchname"). val ()},
Url: sAjaxUrl,
Success: function (data, textStatus) {if (data! = Null ){
If (data. Instance = null & data. Instance. length = 0) {return;
}
Else {
Var datalist = data. Instance; if (sort = 1) {datalist = datalist. sort (
Function (a, B ){
Return (a. Id-B. Id );}
);
} Else {datalist = datalist. sort (
Function (a, B) {return (B. Id-a. Id );}
);
}
Var html = "";
// Bind data to the table
Var tabledata = GetJson (datalist );
}
}
},
Complete: function (XMLHttpRequest, textStatus ){},
Error: function (e ){
}
});
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// If the returned result is:
Var json = "['2017-4-2 ', '2017-4-1', '2017-5-2 ']";
Var dateArray = eval (json );
For (I in dataArray)
{
Document. write (dataArray [I]);
}
</Script>
Or:
Copy codeThe Code is as follows:
$. Each (data. comments, function (I, item ){
$ ("# Info"). append (
"<Div>" + item. id + "</div>" +
"<Div>" + item. nickname + "</div>" +
"<Div>" + item. content + "</div> });
3. Complex Nesting is also an object. Js will correspond to the background completely. You can traverse it.
Most of the time, the background returns to the foreground in json format. Json can be directly parsed into objects in js.