ASP. Net + Jquery.ajax Preliminary Practice

Source: Internet
Author: User

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

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.