I'm wanting to show a "Loading" progress bar every time an ajax request is sent. Is it possible to be notified ANYTIME an ajax request is sent using jQuery?
The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.
$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });
send = function() { console. log( arguments ); //Here is where you can add any code to process the request. //If you want to pass the Ajax request object, pass the 'pointer' below var pointer = this var intervalId = window. setInterval(function(){ if(pointer. readyState !=
The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.
You could use the $.ajaxSetup()
method to set global AJAX properties that will apply for the entire page:
$.ajaxSetup({
beforeSend: function() {
// show progress spinner
},
complete: function() {
// hide progress spinner
}
});
For some reason or another $.ajaxSetup()
wasn't working for me. After reading through the docs though, I found the following:
Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().
Try $.ajaxStart()
and $.ajaxComplete()
instead:
$(document).ajaxStart(function () {
console.log('Request Initiated');
});
$(document).ajaxComplete(function () {
console.log('Request Complete');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With