Jquery+asp.net backstage data to the foreground JS parsing method

Source: Internet
Author: User

Therefore, in parsing the background data, we need to base on the data situation, special treatment and treatment.

I'm using the WCF services provided by ASP. In the background, there are also ashx general handlers. Roughly the same principle.

In C #, we often use objects that have entity objects such as: User;

Complex points also have object nested objects or list collections. But there is no difference, just look at your data how much to decide is to be processed by JS data,

Or is the background processed to return the final result directly.

1, Entity object: Return is the object, in JS, is directly and your background code class object data is the same.

The following code, for example, is getting 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 containing some objects: this is a lot of application scenarios.

In JS, the corresponding array of arrays. In the array is the object entity that you return. Each traversal can be used. For details, refer to:

[jquery operation JS Arrays and examples of objects]

Demo

$.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) {???????????????????
??????????? }
??????? });

<script type= "Text/javascript" >
If the return is:
var json = "[' 2010-4-2 ', ' 2010-4-1 ', ' 2010-5-2 ']";
var Datearray = eval (JSON);
For (i in DataArray)
{
?? document.write (Dataarray[i]);
}
</script>

Or:

? $.each (data.comments, function (I, item) {
??????????? $ ("#info"). Append (
??????????????????? "<div>" + item.id + "</div>" +
??????????????????? "<div>" + Item.nickname??? + "</div>" +
??????????????????? "<div>" + item.content + "</div>??????? });

3, if it is complex nested, it is also the object. JS will be in full and background corresponding. You can traverse it.

Now many times, backstage back to the foreground is generally used JSON. JSON can be directly parsed into objects in JS.

Jquery+asp.net backstage data to the foreground JS parsing method

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.