.ajaxSuccess() JQ Home  <<  Ajax  <<  .ajaxSuccess()

Ajax global success handler.

Description

The .ajaxSuccess() Ajax method, allows us to register an Ajax Event handler to be called whenever an Ajax request completes successfully.

  • The .ajaxSuccess() method callbacks are executed via the ajaxComplete event which is triggered every time an Ajax request completes. For finer grained control of callback execution when the .ajaxSuccess() Ajax method is fired, interrogate the event and jqXHR objects or the ajaxSettings parameters passed to the method.

Syntax

Signature Description
.ajaxSuccess( handler( event, jqXHR, ajaxSettings) )Register an Ajax Event handler to be called whenever an Ajax request completes successfully.

Parameters

Parameter Description Type
handler(event, jqXHR, ajaxSettings) The function to be called.
  • Each time the callback runs, it is passed the event and jqXHR objects as well as the ajaxSettings pertaining to the request.
  • The callback is fired in the context of the current jQuery instance, so the this special operator refers to that instance.
Function

Return

A jQuery object.

.ajaxSuccess(handler( event, jqXHR, ajaxSettings)) Example Ajax  <<  Top

Register an Ajax Event handler to be called whenever an Ajax request completes successfully.

As of jQuery 1.8, the .ajaxSuccess() method should only be attached to document.

In the example below when we press the button the first time, we use the .ajaxSuccess() method to set up a global event handler to be run for each Ajax request. The handler is fired on successful completion of a request and we output a message giving some information about the request.


$(function(){
  $('#btn6').one('click', function(){
    // Register global event handler and attach to 'div6' element
    $(document).ajaxSuccess(function(event, jqXHR, ajaxSettings) {
       $('#div6').append('Triggered the ajaxSuccess() global event handler for url: ' 
                      + ajaxSettings.url + '. Status: ' + jqXHR.status + '. Status text: ' 
                      + jqXHR.statusText + '.<br><br>');
    });
    $.getScript( "../../../js/get9.js" );
    $.post( "../../../js/post6.js" );
  });
});

div6. Some initial text.

Press the button below to action the above code:

Related Tutorials

jQuery Advanced Tutorials - Lesson 11 - Ajax Global Event Handlers