Jquery + asp.net background data is uploaded to front-end js for parsing _ json

Source: Internet
Author: User
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:


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.