Jquery+asp.net background data to the front desk JS analysis of the way to explain

Source: Internet
Author: User
Tags json sort

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.