Jquery Ajax example

Source: Internet
Author: User

Using jquery makes Ajax easier. Jquery provides some functions to make simple work easier and complex work no longer complicated.

The most common use of AJAX is to load an HTML code to a certain area of the page. To do this, simply select the required elements and use the load () function. The following is an example of updating statistics:

('{Stats'{.load('stats.html ');

Generally, we only need to pass some parameters to a page on the server. As you expected, using jquery is very simple. You can use $. Post () or $. Get (), which is determined by the required method. If necessary, you can also pass an optional data object and callback function. Listing 4 shows a simple example of sending data and using callback.

Listing 4. Use ajax to send data to the page

$. Post ('Save. cgi ',{

Text: 'My string ',

Number: 23

}, Function (){

Alert ('your data has been saved .');

});

If you really need to write some complex Ajax scripts, you need to use the $. Ajax () function. You can specify XML, script, HTML, or JSON. jquery automatically prepares suitable results for the callback function, so that you can use the results immediately. You can also specify beforesend, error, success, or complete callback functions to provide users with more feedback on the Ajax experience. In addition, some other parameters are available. You can use them to set the Ajax request timeout, or set the "last modification" status on the page. Listing 5 shows an example of retrieving an XML document using some of the parameters I mentioned.

Listing 5. $. Ajax () makes Ajax complex and simple

$. Ajax ({

URL: 'document. xml ',

Type: 'get ',

Datatype: 'xml ',

Timeout: 1000,

Error: function (){

Alert ('error loading XML document ');

},

Success: function (XML ){

// Do something with XML

}

});

After the success callback function returns an XML document, you can use jquery to retrieve this XML document in the same way as retrieving HTML documents. This makes it quite easy to process XML documents and integrates content and data into your web site. Listing 6 shows an extension of the success function, which adds a list item to the web page for each element in XML.

Listing 6. Using jquery to process XML documents

Success: function (XML ){

$ (XML). Find ('item'). Each (function (){

VaR item_text = $ (this). Text ();

$ ('Lil ')

. Html (item_text)

. Appendto ('ol ');

});

}

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.