We often use jquery to read background data and return data in the background. There are many background data formats, but there are no different types in js. Therefore, when parsing background data, we need to handle and treat 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.
The 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:
The 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 ){
}
});
The Code is as follows: