jquery's built-in AJAX features and JSON usages _jquery

Source: Internet
Author: User

Through jquery's built-in AJAX capabilities, direct access to the background to obtain JSON-formatted data, and then through the Jquer data binding to the previously designed HTML template, directly on the page display.
Let's look at the HTML template first:

<table id= "Datas" border= "1" cellspacing= "0" style= "border-collapse:collapse" >
<tr>
<th>
order id</th>
<th>
customer id</th>
<th>
employee id</th>
<th>
Order Date </th>
<th>
ship date </th>
<th>
shipper name </th>
<th>
Shipper's address </th>
<th>
Shippers City </th>
<th>
More information </th>
</tr >
<tr id= "template" >
<td id= "OrderID" >
</td>
<td id= "CustomerID" >
</td>
<td id= "EmployeeID" >
</td>
<td id= "OrderDate" >
</td >
<td id= "ShippedDate" >
</td>
<td id= "Shippedname" >
</td>
<TD id= "shippedaddress" >
</td>
<td id= "shippedcity" >
</td>
<td id= "More" >
</td>
</tr>
</table>

It must be noted that all the ID attributes inside, this is a key. Take a look at the code for AJAX requests and binding data.

 $.ajax ({type: "get",//Use the Get method to access the background dataType: "JSON",//returns the data URL in JSON format: "Backhandler.ashx
",//to access the background address data:" pageindex= "+ pageindex,//to be sent Complete:function () {$ (" #load "). Hide (),},//ajax hidden loading prompt upon completion of request Success:function (msg) {//msg for the returned data, here do the data-bound Var: msg.table, $.each (data, function (I, n) {var row = $ ("#template"). Cl
One ();
Row.find ("#OrderID"). Text (n. Order ID);
Row.find ("#CustomerID"). Text (n. Customer ID);
Row.find ("#EmployeeID"). Text (n. Employee ID);
Row.find ("#OrderDate"). Text (ChangeDate (n. Order date));
if (n. Ship date!== undefined) row.find ("#ShippedDate"). Text (ChangeDate (n. Ship date));
Row.find ("#ShippedName"). Text (n. Shipper name);
Row.find ("#ShippedAddress"). Text (n. Shipper's address);
Row.find ("#ShippedCity"). Text (n. Shipper city); Row.find ("#more"). HTML ("<a href=orderinfo.aspx?id=" + N. Order ID + "&pageindex= +pageindex+" > more</a 
> "); 
ROW.ATTR ("id", "Ready");//Change the ID row.appendto ("#datas") of the row that binds the data;//Add to the template's container}); 

This is a jquery Ajax method, the return data is not complex, mainly explain how the data by the definition of the template to appear on the page. The first is this "var row = $ (" #template "). Clone ();" Copy the template first, then Row.find ("#OrderID"). Text (n. Order ID), which indicates that the Id=orderid tag is found, the innertext of it is set to the appropriate data, and, of course, it can be formatted as HTML data. Or you can use an external function to convert the data to the desired format, such as Row.find here ("#OrderDate"). Text (ChangeDate (n. Order date)); Some server controls feel like template binding data.
All of these, are placed on a static page, only through the Ajax method to get the data from the background, all the HTML code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

Pagedata.js is to include the above Ajax request and binding data code JS, the entire page even the form does not need to do so what is the benefit. And look at one of the following templates

<ul id= "Datas" >
<li id= "template" >
<span id= "OrderID" >
fsdfasdf
</span>
<span id= "CustomerID" >
</span>
<span id= "EmployeeID" >
</span>
< Span id= "OrderDate" >
</span>
<span id= "ShippedDate" >
</span>
<span ID = "Shippedname" >
</span>
<span id= "shippedaddress" >
</span>
<span id= "Shippedcity" >
</span>
<span id= "More" >
</span>
</li>
</ Ul>

Still pay attention to the id attribute. You see here should understand, no matter what form of expression, as long as the id attribute the same, you can bind the data to the corresponding location. In this way, we do the program will not be modified by the artist to modify the code, and art also as long as the HTML can be done, do not need to make a template for server controls (but I have not encountered such art, are art design I have to change to the server control template).

The background of the AJAX request again, with the access of the Northwind database, the Order table into the DataTable, and then through the Datatable2json into the JSON data format passed back to the end, but the background used some paging and caching methods, Hope to have some help for beginners.

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.