JQuery's built-in AJAX function and JSON usage example _ jquery

Source: Internet
Author: User
Use jQuery's built-in AJAX function to access the background to obtain JSON-format data, and then display the data directly on the page through jQuer. If you need it, refer to the built-in AJAX function of jQuery, directly access the background to obtain data in JSON format, and then bind the data to the pre-designed html template through jQuer, which is directly displayed on the page.
Let's take a look at the html template:

 
 
Order ID Customer ID Employee ID Order Date Date of shipment Owner name Cargo owner address Cargo owner City More information

All the id attributes are important. Let's take a look at the AJAX request and data binding code.

$. Ajax ({type: "get", // use the get method to access the background dataType: "json", // return the json format Data url: "BackHandler. ashx ", // the backend address to be accessed data:" pageIndex = "+ pageIndex, // The data to be sent complete: function () {$ (" # load "). hide () ;}, // when the AJAX request is complete, hide the loading prompt success: function (msg) {// msg as the returned data. Bind var data = msg here. table; $. each (data, function (I, n) {var row = $ ("# template "). clone (); row. find ("# OrderID "). text (n. order ID); row. find ("# CustomerID "). text (n. customer ID); row. fi Nd ("# EmployeeID "). text (n. employee ID); row. find ("# OrderDate "). text (ChangeDate (n. order Date); if (n. delivery date! = Undefined) row. find ("# ShippedDate "). text (ChangeDate (n. delivery Date); row. find ("# ShippedName "). text (n. goods owner name); row. find ("# ShippedAddress "). text (n. cargo Master Address); row. find ("# ShippedCity "). text (n. cargo Main City); row. find ("# more" detail .html ("More"); row. attr ("id", "ready"); // Changes the idrow of the row bound to the data. appendTo ("# datas"); // Add it to the container of the template });

This is jQuery's AJAX method, and the returned data is not complex. It mainly describes how to display the data to the page according to the template definition. The first is the "var row = $ (" # template "). clone (); "first copy the template, and then row. find ("# OrderID "). text (n. order ID);, indicating to find the tag id = OrderID, set its innerText as the corresponding data, of course, you can also set it to html format data. Or use external functions to convert the data into the required format, for example, row. find ("# OrderDate "). text (ChangeDate (n. order Date); a bit of server controls do the feeling of template binding data.
All of these are put in a static page, and only the AJAX method is used to obtain data from the background. All the html code is as follows:

Test1
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.