Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to stop multiple jQuery ajax error handlers?

A team member put this into our project

$(function() {
    $("body").bind("ajaxError", function(event, XMLHttpRequest, ajaxOptions, thrownError){
        alert(thrownError);
    });
}

However I want to supress one of my errors (as it would be noise and verification isn't needed)

function blah() {

    ...

    errFunc = function(event, xhr, opts, errThrown) {
        //what could I do here?
        //event.stopImmediatePropagation()?
    }

    $.ajax({
        url        : '/unimportant_background_refresh',
        type       : 'GET',
        data       : { },
        dataType   : 'json',
        success    : updateBackgroundStuff,
        error      : errFunc,  // Suppresses error message.
    });

}

How can I stop the catch all error from happenning please? Can I just do something in the error function such as { event.StopPropogation(); } or must I work out some mechanism for having the catch all selectively ignore things please?

like image 375
Philluminati Avatar asked Nov 08 '11 13:11

Philluminati


People also ask

How do I stop multiple AJAX requests?

Maintain a variable isLoading that stores the status of the request. Only if the request is not loading (i.e., there's no AJAX request in progress), do we start a new request. Otherwise, we just ignore the new click event.

How do I stop AJAX calls?

Just call xhr. abort() whether it's jquery ajax object or native XMLHTTPRequest object.

When a jQuery AJAX operation fails the global event can be invoked?

version added: 1.0.Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

What causes AJAX errors?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.


1 Answers

Global events can be disabled, for a particular Ajax request, by passing in the global option, like so:

 $.ajax({
   url: "test.html",
   global: false,
   // ...
 });

Taken from: http://docs.jquery.com/Ajax_Events

like image 127
Variant Avatar answered Nov 15 '22 16:11

Variant