简体   繁体   中英

Alert on HTTP request

In jquery one can do an event like this:

$( "#target" ).click(function() {
  alert( "Handler for .click() called." );
});

Is it possible to do this when a page is doing a HTTP request to a certain URL? Maybe with plain Javascript? It shouldent matter what kind of request (ea AJAX or post or whatever)

$( "#target" ).???ON_HTTP_REQUEST???(function() {
  alert( "Handler for .click() called." );
});

I Googled a LOT but there is nothing on this.

What you're looking for is probably $.ajaxStart , which fires whenever an ajax call starts up (POST and GET):

$( document ).ajaxStart(function() {
    alert("Ajax started");
});

from the docs:

Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.

$( document ).ajaxSend(function( event, jqxhr, settings ) {
  if ( settings.url == "ajax/test.html" ) {
    alert( "loading ajax/test.html" );
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM