Example analysis of Ajax asynchronous invocation

Source: Internet
Author: User
Tags html tags string tags

Ajax more focused on the front page asynchronous display of the Background database field, the main idea is similar to the previous article, focusing on the DAL layer to get stored in the process of the table collection dataset, in general stored procedures when stitching strings, the table row of data into the form of key value pairs

1. Front page contains script (AJAX) and basic HTML tags to facilitate the search and understanding

The code is as follows Copy Code
function InitData () {//page load Call this event
$.ajax ({
Type: "POST",
URL: "/ajaxrequest/getuserlist.ashx",//link to the corresponding generic handler
Data: {state:1},
Success:function (data) {
if (data!= null) {
var html = "<li ><span class=" LB1 >ID</span><span> username </span><span > creation time </span                         ><span > Perform operation </span></li> "; Splice string tags and put them in variable HTML, note: stitching "needs to be escaped-like the class in the first span
$.each (data, function (I, n) {//jquery each row of the output list, if you use JavaScript to consider using a for loop
HTML + + "<li><span class=" LB1 ">" + (1 + i) + "</span><span >" + n.user_name + "</span><s Pan > "+ n.user_time + </span><span ><a href=" javascript:editprovince (' "+ this. User_name + "'," + this. user_id + "," + (i + 1) + ")" >[edit]</a></span><span ><a href= "javascript:deleteprovince" + this. USER_ID + ");" onclick= "Javascript:return confirm (' You are sure you want to delete" +this. User_name+ "?"); >[delete]</a></span></li> ";
Still stitching to show the mark, note: variable html+=, in addition, if it is part of the content or a different part of each line to use the variable
Variable with data[i]. Key (You can get the value of the key in the JSON object in the file that the URL points to), which is the key value pair when the JSON object is spliced in the generic handler file.
n here represents Data[i] (is the jquery from each usage, if it is in JavaScript can use Data[i]. USER_ID)
});
$ ("#list_ul"). Append (HTML); Get the UL to display the list, add the append label string with the

}
else {
Alert ("error!");
}
}
})
}

2. General handler Getuserlist.ashx, converts data from the underlying dataset into a JSON object and returns it to the calling page

The code is as follows Copy Code

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
string result = "[";
String name=string. Empty;
Int? state = null;
DateTime time;
Int id=0;
if (!string. IsNullOrEmpty (context. request["State"))
State = Convert.ToInt32 (context. request["State"]);
DataSet _set=new BLL. Province (). Getprovincelist (state);
foreach (DataRow Dr in _set. Tables[0]. Rows)
{
Name=convert.tostring (dr["user_name"]);
Id=convert.toint32 (dr["user_id"]);
Time=convert.todatetime (dr["User_time"]);
result+= "{" user_name ":" "+name+" "," user_id ":" "+id+" "," User_time ":" "+time+" "},";
}
resul= result. Trim (",") + "]";
Context. Response.Write (Result);
}

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.