May use jquery Ajax to pass a value to the background of a string, or the serialized form has been used by everyone, but some items, we need to pass the value of an array to the background, what is the best way to do this?
1.JS converts an array to a string, then passes the value to the background, inconvenient
For example, a [1,2,3,4,5] array, converted to a var str= "1,2,3,4,5"; string, which is then passed to the background, separated by The. net split (', '), which is awkward to use, and also to consider whether there is a problem with the split string in the array
2. Use the json.stringify () method, convenient, Simple and Rough.
Last night, Xiao Chu encountered the problem of submitting an array, so and friends of the small fly to discuss the next, before I know that jquery has stringify () This method, also has not known how to use, yesterday little fly know the problem of small zhu, teach the next small Zhu this method, solve the problem of submitting an array
Now learn to sell, so today and the park friends to share this practical method
first, introduce the necessary jquery file
Then get an array from your page, depending on your actual needs, may be user input, or you can crawl from the table table
For example, the need to grab a similar school number, the current side of the checkbox is selected, put this number, put in the array
Now that the ink has a name Arrid array, of course you can also use other methods to piece together their own arrays, this is not the focus of this article, the focus of this article is below, when you get this array, such as after passing to the background, and background interaction
To pass an array, the above is to put together such an array:
After we get this array, we use Ajax to pass to the Background:
// $.ajax ({type: " Post ", url: "manager.aspx/deletemore" data:json.s Tringify ({arr:arrid}), contentType: "application/json;charset=utf-8" , dataType: "json" function (data) { if (DATA.D = = "Y" "bulk Delete succeeded!) "); Window.location.reload (); } } });
so, we need to backstage in this page, write a WebMethod (this page of my name is called manage.aspx, please reader friends according to their own page remember the corresponding changes)
[WebMethod] public Static stringDeletemore (list<string>Arr) {StringBuilder SB=NewStringBuilder (); foreach(varIteminchArr) {sb. Append (item+","); } sb. Remove (sb.) Length-1,1); stringSqldelete ="UPDATE stuapply_info SET status=1 WHERE Applyid in ("+ sb. ToString () +")"; intR =DBUTILITY.DBHELPERSQL.EXECUTESQL (sqldelete); if(r >0) { return "Y"; } Else { return "N"; } }
WebMethod need to introduce namespaces:
using System.Web.Services;
then, Press F5 to run Normally.
=========== Summary ============
The above may be said to be more verbose, because the young Zhu worry that friends can not understand, the following simple to do a summary:
1. Introduction of JS file
2. piece together an array
3.ajax Background interaction
$.ajax ({type:"Post", Url:"page Name. aspx/load", data:JSON.stringify ({arr:arrid}), contentType:"Application/json;charset=utf-8", DataType:"JSON", Success:function (data) {alert ("Operation successful! "); } } });
4. Background introduction of namespaces, write WebMethod
[WebMethod] public Static stringLoad (list<string>Arr) {StringBuilder SB=NewStringBuilder (); foreach(varIteminchArr) {sb. Append (item+","); } sb. Remove (sb.) Length-1,1); //Sb. ToString () for 1,2,3,4 can be used in the where of SQL statements ... In ...//Of course you can also use list<string> depending on your business logic//Remember to return "Y" at the end;}
The use of the method to record down, share to friends, convenient for friends to learn, convenient to review their own later!
JQuery Ajax asynchronously passes an array to the. net background