I have a page and I would like to disable all AJAX requests with jQuery.
Do you have any ideas? And if it is possible?
if (false) {
//disable all ajax requests
}
If isLoading is false, the AJAX call starts, and we immediately change its value to true. Once the AJAX response is received, we turn the value of that variable back to false, so that we can stop ignoring new clicks.
click(function(e) { e. preventDefault(); if ( $(this). data('requestRunning') ) { return; } $(this). data('requestRunning', true); $.
To abort a previous Ajax request on a new request with jQuery, we can call the abort method on the request object. For instance, we can write: const currentRequest = $. ajax({ type: 'GET', url: 'https://yesno.wtf/api', }) $.
If all of your ajax requests are being sent through jQuery ajax methods (including helper methods), you can do this with beforeSend.
window.ajaxEnabled = true;
$.ajaxSetup({
beforeSend: function(){
return window.ajaxEnabled;
}
});
$.post("http://www.google.com"); // throws an error
window.ajaxEnabled = false;
$.post("http://www.google.com"); // doesn't throw an error
http://jsfiddle.net/k2T95/3
And here's one that will block all, regardless of what javascript library is sending it, also based on a global flag. Doesn't affect XDomainRequest obj though
(function (xhr) {
var nativeSend = xhr.prototype.send;
window.ajaxEnabled = true;
xhr.prototype.send = function () {
if (window.ajaxEnabled) {
nativeSend.apply(this, arguments);
}
};
}(window.XMLHttpRequest || window.ActiveXObject));
http://jsfiddle.net/k2T95/4/
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