.ajaxSend()S2C Home « Ajax « .ajaxSend()

Ajax global send handler.

Description

The .ajaxSend() Ajax method, allows us to register an Ajax Event handler to be called before Ajax requests are sent.

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

Syntax

Signature Description
.ajaxSend( handler(event, jqXHR, ajaxSettings) )Register an Ajax Event handler to be called before Ajax requests are sent.

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.

.ajaxSend( handler(event, jqXHR, ajaxSettings) ) ExampleTop

Register an Ajax Event handler to be called before Ajax requests are sent.

In the example below when we press the button the first time, we use the .ajaxSend() method to set up a global event handler to be run for each Ajax request. The handler is fired every time an Ajax request is about to be sent. In this case the handler outputs a message giving some information about the request to be sent.


$(function(){
  $('#btn3').one('click', function(){
    // Register global event handler and attach to 'div3' element
    $('#div3').ajaxSend(function(event, jqXHR, ajaxSettings) {
       $(this).append('Triggered the ajaxSend() global event handler for url: ' 
                      + ajaxSettings.url + '.<br><br>');
    });
    $.ajax( "../js/get3.js", {
      dataType: "script",
      success: function() { 
        $('#div3').append('The request was a success! <br>');
      }
    });
    $.ajax( "../js/get33.js", {
      dataType: "script",
      success: function() { 
        $('#div3').append('The request was a success! <br>');
      }
    });
    $.get( "../js/get5.js" );
    $.post( "../js/post3.js" );
  });
});

div3. Some initial text.

Press the button below to action the above code: