Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all AJAX requests in a page with jQuery and Javascript ?

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
}
like image 439
user3669206 Avatar asked May 23 '14 14:05

user3669206


People also ask

How do I stop multiple AJAX calls?

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.

How do I stop multiple AJAX calls from repeated clicks?

click(function(e) { e. preventDefault(); if ( $(this). data('requestRunning') ) { return; } $(this). data('requestRunning', true); $.

How can I cancel old AJAX request?

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', }) $.


1 Answers

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/

like image 118
Kevin B Avatar answered Oct 12 '22 12:10

Kevin B