Home  >  Article  >  Web Front-end  >  Jquery asp.net background data is transferred to the front-end js for analysis_json

Jquery asp.net background data is transferred to the front-end js for analysis_json

WBOY
WBOYOriginal
2016-05-16 16:48:531526browse

So when parsing the background data, we need to process and treat it specially according to the background data situation.

What I use in the background here is the wcf service provided by asp.net, and there is also an ashx general processing program. The general principle is almost the same.

The objects we often use in C# include entity objects such as User; there are List collections, which generally return lists.

Somewhat more complicated are objects nested in objects or list collections. But there is no difference. Just look at the size of your data to decide whether js will process the data,

It is still processed in the background and returns the final result directly.

1. Entity object: If the returned object is an object, in js, it is directly the same as the object data in your background code class.

For example, the following code obtains an object. You can get it directly by using its name attribute.

Copy code The code is as follows:

$.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); 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 collection, including some objects: there are many application scenarios for this.

In js, the corresponding array array. The array contains the object entities you return. You can use each traversal. For details, please refer to:

[Jquery operation js array and object examples]

demo:

Copy code The code is as follows:

$.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 = "";                                                                                                                                          XMLHttpRequest, textStatus) { 🎜>




Copy code


The code is as follows:


or:





Copy code


The code is as follows:

$.each(data.comments, function(i, item) {

                                                                                                                    < /div>"

"

" item.content "

");

}); 3. If it is complex nested, it is also an object. js will completely correspond to the background. You can just traverse it. Nowadays, json is usually used from the background to the front desk. json can be directly parsed into objects in js.
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
Previous article:Implement keyboard key monitoring based on Jquery_jqueryNext article:Implement keyboard key monitoring based on Jquery_jquery

Related articles

See more