Ajax Global Events in jquery

Source: Internet
Author: User
Tags ming

jquery is very powerful and convenient in terms of Ajax, and here's how jquery makes Ajax requests when it comes to the method template:

  1. $. Ajax({
  2. type: "get",
  3. URL: " ",
  4. Data : {},
  5. beforesend : function() {
  6. },
  7. Success : function(data) {
  8. },
  9. Complete : function() {
  10. }
  11. });

The use of the $.ajax () method is not the purpose of the Ming River writing this article, today the Ming River mainly explains the complete event flow when making Ajax requests.

All global events for the Ajax method of jquery:
    • Ajaxstart:ajax before the request begins
    • Ajaxsend:ajax when requested
    • Ajaxsuccess:ajax after getting the data
    • Ajaxcomplete:ajax when the request is complete
    • Ajaxerror:ajax after an error has occurred on the request
    • Ajaxstop:ajax after a request is stopped

When you use jquery's ajax approach, both $.ajax (), $.get (), $.load (), $.getjson (), and so on, will trigger global events by default, but they are usually not bound to global events, but these global events are actually very useful.

The use of global events for AJAX methods

Ajax global events, there is a typical application scenario:
There are several or even a few AJAX requests on your page, but these AJAX requests have the same message mechanism. A prompt is displayed before the AJAX request begins, prompting "reading data", and the Prompt box displays "Data acquisition success" when the Ajax request succeeds, and hides the prompt after the AJAX request ends.
The practice of not using global events is:
Add the Beforesend, success, and complete callback functions to $.ajax (), and add the processing hints to the callback function.
The practice of using global events is:

  1. $(document). Ajaxstart(onStart)
  2. . ajaxcomplete(oncomplete)
  3. . ajaxsuccess(onsuccess);
  4. function onStart(event){
  5. //.....
  6. }
  7. function oncomplete(event,xhr, settings){
  8. //.....
  9. }
  10. function onsuccess(event,xhr, settings){
  11. //.....
  12. }
Ajax method Complete Event flow

For more intuitive explanation, Ming River use axure draw two flow chart, draw bad also please forgive, O (∩_∩) o

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.