We often use jquery to read background data, back to the background data. Background data format has a lot of, but JS inside there is no type of points.
Therefore, when parsing the background data, we need to according to the background of the data, special treatment and treatment.
I'm here backstage with the WCF service provided by ASP.net, as well as the ASHX general handler. Roughly the same principle.
In C # We often use objects, there are entity objects such as: User, there are list collection, generally return list.
Complex points also have object nesting objects or list collections. But no difference, just look at your data how much to decide is by JS processing data,
or the background processing directly return the final result.
1, Entity objects: Return is the object, in JS, directly and your background code class object data is the same.
such as the following code, is to get to an object. It can be obtained directly with its Name property.
$.ajax ({
Type: "Post",
DataType: "JSON",
Traditional:true,
Data: {
Oper: "Edit",
Sid:id
},
Url:ajaxurl,
Success:function (data, textstatus) {
if (data!= null) {
if (data) {
$ ("#name"). Val (data. Name), gets to the object. 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 return data is a list collection, contains some objects: This scenario is also many.
In JS, the corresponding array of arrays. In the array is the object entity you are returning. You can use each traversal. Specific can refer to:
$.ajax ({type: "Post", DataType: "JSON", Traditional:true, data: {oper: "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 table var Tabledata = Getjson (DataList); }}, Complete:function (XMLHttpRequest, Textstatus) {}, Error:function (e) {}});
3, if it is a complex nesting, it is also the object. JS will be completely and backstage correspondence. You can traverse it.
Now many times, backstage back to the front desk is generally used in JSON. JSON can be parsed directly into objects in JS.