jquery's ajax--global Ajax event handler

Source: Internet
Author: User

1. These methods are used to register the event handler to handle any AJAX requests on the page, which are invoked when certain events are triggered. If the properties in Jquery.ajaxsteup () global are set to true (which is also the default setting), then each AJAX request triggers a global event. Note: Global events are never triggered by cross-domain (cross-domain) scripts or JSONP requests, and global the setting of properties is irrelevant.

2.. ajaxcomplete () method: Whenever an AJAX request is completed, jquery triggers the ajaxComplete event, at which point all the processing functions are .ajaxComplete() registered and executed using the method. If the global property is set to False, this method is not called when the AJAX request is completed.

No matter which Ajax request is completed, all ajaxComplete processing functions will be executed. If we have to distinguish between different requests, we can pass the arguments to this handler function. He is performed by the event object, the XMLHttpRequest object and the request that is used in the Settings object, every time the ajaxComplete processor is executed. For example, we can limit our callbacks to handling only the event to handle a particular URL:

$ (document). Ajaxcomplete (function (event, XHR, Settings) {if (Settings.url = = = "Ajax/test.html") {$ (". Log"). Text ("Tr Iggered Ajaxcomplete handler. The result is "+ xhr.responsehtml);});

when the AJAX request is complete, a message is displayed:

$ (document). Ajaxcomplete (function (event,request, settings) {$ ("#msg"). Append ("<li> request completed. </li> "); });         

3. Ajaxerror () method: Whenever an AJAX request goes wrong, jquery triggers the ajaxError event, at which point all the processing functions are .ajaxError() registered and executed using the method. Note: the handler processor here invokes cross-domain scripting and cross-domain JSONP requests. If the global property is set to False, this method is not called when an AJAX request fails to complete.

No matter which Ajax request is completed, all ajaxError processors will be executed. If we have to distinguish between different requests, we can pass the parameters to the processor. He is the request that is used by the event object, XMLHttpRequest object, and Settings object, each time the ajaxError processor executes, it passes the event object, the jqXHR object (before JQuery 1.5 is the XHR object), and the setting (settings) object used to create the request. If the request fails because JavaScript throws an exception, and the exception object as the fourth argument is passed to the handler function. For example, we can limit our callbacks to handling only the event to handle a particular URL:

$ (document). Ajaxerror (function (event, JQXHR, settings, exception) {  if (Settings.url = = "Ajax/missing.html") {
   
    $ ("Div.log"). Text ("triggered Ajaxerror handler.");  });
   

displays a message when the AJAX request fails:

$ (document). Ajaxerror (function (event, request, Settings) {  $ ("#msg"). Append ("<li>error requesting page" + S Ettings.url + "</li>");});

4.. ajaxsend () method: Whenever an AJAX request is about to be sent, jquery triggers the ajaxSend event, at which point all the processing functions are .ajaxSend() registered and executed using the method. If the global option is set to false , the call $.ajax() or $.ajaxStep() method will not be triggered.

No matter which Ajax request is sent, all ajaxSend processors will be executed. If we have to distinguish between different requests, we can pass the parameters to the processor. Each time ajaxSend the processor executes, it passes the event object, the jqXHR object (which is the object in JQuery 1.4 XMLHttpRequest ), and the Set (Settings Object) object used to create the request. If the request fails because JavaScript throws an exception, and the exception object as the fourth argument is passed to the handler. For example, we can limit our callbacks to handling only the event to handle a particular URL:

$ (document). Ajaxsend (function (event, JQXHR, settings) {  if (Settings.url = = "Ajax/test.html") {    $ (". Log"). T Ext ("triggered ajaxsend handler.");}  );

displays a message before the AJAX request is sent:

$ (document). Ajaxsend (function (event, request, Settings) {    $ ("#msg"). Append ("<li>starting request at" + Sett Ings.url + "</li>");});

5. Ajaxstart () method: Whenever an AJAX request is about to be sent, jquery checks for any other AJAX requests in the response process (note: Outstanding requests). If not checked, jquery triggers the ajaxStart event, at which point all processing functions are .ajaxStart() registered and executed using the method. If the global option is set to false , the call $.ajax() or $.ajaxStep() method will not be triggered.

displays a message when the AJAX request begins to be sent (no AJAX request has been activated):

$ (document). Ajaxstart (function () {   $ ("#loading"). Show ();});

6. Ajaxstop () method: Whenever an AJAX request is completed, jquery checks for any other AJAX requests in the process (note: Outstanding requests). If all is done, jquery triggers the ajaxStop event, at which point all processing functions are .ajaxStop() registered and executed using the method. If an unhandled Ajax request returns a cancellation with a beforeSend callback function false , the ajaxStop event is also triggered. If the global option is set to false , the call $.ajax() or $.ajaxStep() method will not be triggered.

hide the loading information after the AJAX request stops:

$ (document). Ajaxstop (function () {      $ ("#loading"). Hide ();});

7.. ajaxsuccess () method: Whenever an AJAX request is completed successfully, jquery triggers the ajaxSuccess event, at which point all the processing functions are .ajaxSuccess() registered and executed using the method. If the global option is set to false , the call $.ajax() or $.ajaxStep() method will not be triggered.

No matter which Ajax request is completed, all ajaxSuccess processors will be executed. If we have to distinguish between different requests, we can pass the parameters to the processor. He is performed by the event object, XMLHttpRequest object, and the request that is used in the Settings object, every time the ajaxSuccess processor executes. For example, we can limit our callbacks to handling only the event to handle a particular URL:

$ (document). Ajaxsuccess (function (event, XHR, settings) {  if (Settings.url = = "Ajax/test.html") {    $ (". Log"). Te XT ("triggered ajaxsuccess handler. The AJAX response was: "+                      Xhr.responsetext);  }});

When the AJAX request completes successfully, a message is displayed:

$ (document). Ajaxsuccess (function (event, request, Settings) {   $ ("#msg"). Append ("<li>successful request! </li> "); });

jquery's ajax--global Ajax event handler

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.