asp.net using jquery Ajax+webservice+json implementation code for no flush background value _jquery

Source: Internet
Author: User
Tags eval flush
First of all, put in jquery Ajax:
Copy Code code as follows:

$.ajax ({
URL: ' Ws_ajax.asmx/binddictbyupper ',
Type: ' POST ',
ContentType: ' Application/json;charset=utf-8 ',
DataType: ' JSON ',
Data: ' {ppareid: ' + varlue + '} ',
Success:function (data) {
var dataobj = eval ("+ Data +"));
$ (' #myList '). html (');
for (var i = 0; i < msg.d.length; i++) {
$ (' #myList '). Append (' <li> ' + msg.d[i] + ' </li> ');
// }
alert (dataobj);
$.each ({name: "John", Lang: "JS"}, function (i, n) {
Alert ("Name:" + i + ", Value:" + N);
});
$.each (dataobj, function (idx, item) {
if (idx = = 0) {
return true; Same countinue, return false with break
}
Alert ("Name:" + item[' name '] + ", Value:" + item[' value '));
});
},
Data: ' {query: ' + $ (' #editQuery '). Val () + ' "} ',
Processdata:false
});

Good, the front desk is good, builds a WebService page, writes the method:
Copy Code code as follows:

<summary>
DataTable converted to JSON
</summary>
<param name= "Table" >datatable object </param>
<returns>json string </returns>
public static string Tojson (DataTable Adt)
{
StringBuilder jsonstring = new StringBuilder ();
Jsonstring.append ("[");
foreach (DataRow pdr in adt.rows)
{
Jsonstring.append ("{");
Jsonstring.appendformat ("name:\" {0}\), value:\ "{1}\", pdr["dictionary contents"]. ToString (), pdr["ordinal"]. ToString ());
Jsonstring.append ("},");
Jsonstring.appendformat ("{name: ' {0} ', value: ' {1} '},", pdr["dictionary contents"]. ToString (), pdr["ordinal"]. ToString ());
}
Jsonstring.remove (jsonstring.length-1, 1);
Jsonstring.append ("]");
return jsonstring.tostring ();
}

Then try to run, this is the problem column, no matter how the operation can not be transferred to the background method:
Last time I checked on the Internet.
Found in the new WebService page is missing a key words:

Copy Code code as follows:

[System.Web.Script.Services.ScriptService]


It's OK to add this sentence in front of the class.
Plus later run, OK, can be transferred back to the background
Backstage data is also sent to the foreground
But there is a problem column, the data sent over can not parse out the JSON data
According to the Internet, as long as the AJAX datatype set to JSON on the line, but actually not, and then check the Internet:
1. For the JSON string returned by the server, if the jquery asynchronous request does not have a type description, or is accepted as a string, it needs to be done at once, either in a cumbersome manner or in an eval () to execute the string once
2. For the JSON string returned by the server, the eval () method is not required if the jquery asynchronous request sets the type (typically this configuration property) to "JSON" or uses the $.getjson () method to get the server back. Because the result is already a JSON object, just call the object directly, and here take the $.getjson method as an example to illustrate the method of data processing
But I set his datatype to JSON, should I just use le?
So I put the data back in the eval () method, and then I can call it?
So the data from the front desk can be processed directly in the way you need it.
I have another question here:
I use this way to get the number why I can't find the data (I'm not very familiar with JSON, for the first time):
Copy Code code as follows:

$.each (Data.root,function (Idx,item) {
if (idx==0) {
Return true;//with Countinue, returns false with break
}
Alert ("Name: +item.name+", Value: "+item.value");
});
});

Online look at such a way page can call JSON data, know to answer ...
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.