Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the requested url from ajaxStart function?

Tags:

jquery

Can I get the requested URL from ajaxStart ? I want to execute a common action for all ajax request accept some few requests.

like image 478
palAlaa Avatar asked Sep 02 '12 10:09

palAlaa


2 Answers

No, within ajaxStart you do not have access to the jqXHR object nor the ajaxOptions:

// Watch for a new set of requests
if ( s.global && jQuery.active++ === 0 ) {
  jQuery.event.trigger( "ajaxStart" );
}

As you can see, there are no arguments being passed to ajaxStart. Contrast this with ajaxSend:

// Send global event
if ( fireGlobals ) {
  globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] );
}

Where the jqXHR object is being passed as an argument along with the settings:

$(document).ajaxSend(function(evt, request, settings) {
  alert("Starting request at " + settings.url + ".");
});

Also see this answer for a better understanding of this design.

like image 91
João Silva Avatar answered Oct 09 '22 13:10

João Silva


You cant get requested url from ajaxstart function,you can get only using ajaxsend function,because ajaxsend function regarding to the particular request but ajaxstart is not

$( document ).ajaxSend(function( event, jqxhr, settings ) {
  if ( settings.url == "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxSend handler." );
  }
});
like image 32
Bosco Michael Avatar answered Oct 09 '22 12:10

Bosco Michael