Recently in a Web site project, there is the need for asynchronous requests, after some looking for information, finally realized this part, in this record, for future review.
1.jQuery Ajax functions make it easy to initiate asynchronous requests
$ (". Drop-a"). Click (function () { varContent = $ ( This). Next ("div")); if(Isget = =false) { varCode = $ ("#code"). html (); vardata = {code:code, table: "Acount" }; $.ajax ({type:"POST", URL:".. /response/getinfo.ashx ", Data:data, DataType:"Text", Success:function(msg) {//$ ("#ajax"). Append (msg);alert (msg); Isget=true; } }); }
This example calls: /response/getinfo.ashx (General handler)
2.GETINFO.ASHX Content
Public voidProcessRequest (HttpContext context) {stringCode = Context. request["Code"]; stringTable = context. request["Table"]; stringsql =string. Format ("select * from ' {0} ' where ' {1} ' = ' {2} '", table,"Code", code); DataTable getTable= basicquery.executedatatable (SQL);//call the MySQL query function stringJSON = Jsonconvert.serializeobject (getTable);//Convert object to JSON textContext. Response.ContentType ="Text/plain"; Context. Response.Write (JSON);//response output JSON text}
getinfo.ashx Content
Receives Code and table names as a parameter query database and responds to output a string (JSON text)
3.jquery.ajax processing of returned JSON data
$.ajax ({type:"POST", URL:".. /response/getinfo.ashx ", Data:data, DataType:"Text", Success:function(msg) {//$ ("#ajax"). Append (msg); //alert (msg); varJSON =$.parsejson (msg); varhtml = ""; HTML+ = "<p>id:" + json[0].id; HTML+ = "</p><p>code:" + json[0]. Code; HTML+ = "</p><p>name:" + json[0]. Name; HTML+ = "</p><p>phone:" + json[0]. Phone; HTML+ = "</p><p>mail:" + json[0]. Mail; HTML+ = "</p>"; content.html (HTML); Isget=true; } });
processing the returned JSON string
In this code, the returned JSON string is first converted to an object (array), and then the information that is needed is parsed and loaded into the Web page based on the contents of the returned data.
ASP. Net + Jquery.ajax Preliminary Practice