Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I intercept ajax responses in jQuery before the event handler?

Tags:

jquery

ajax

I'd like to register a global event handler for all AJAX requests, so that I can intercept and handle responses from the server before the specific event handler gets them.

For example, my code might have something like:

$("#placeholder").load("/fragments/userpics");

And I'd like to register a "before" event handler so that I could, for example, display a login box if a 401 response is returned, or maybe retry if there's a 503 response.

I thought that $.ajaxError() is where I would do something like this, but apparently it is only triggered after the event handler.

UPDATE: OK, here's what I got so far, with the help of @genesis:

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
    var success = options.success;
    options.success = function(data, textStatus, jqXHR) {
        // override success handling
        if(typeof(success) === "function") return success(data, textStatus, jqXHR);
    };
    var error = options.error;
    options.error = function(jqXHR, textStatus, errorThrown) {
        // override error handling
        if(typeof(error) === "function") return error(jqXHR, textStatus, errorThrown);
    };
});

After doing some testing, it looks like I would also need to override options.complete.

However, this still doesn't cover all the bases, since you can also attach events directly to the jqXHR object, and changing options won't help in this case.

Any tips?

like image 744
itsadok Avatar asked Aug 31 '11 11:08

itsadok


People also ask

Which global event is triggered if an AJAX request?

The ajaxStart and ajaxStop events are events that relate to all Ajax requests together. This event is triggered if an Ajax request is started and no other Ajax requests are currently running.

Can jQuery and AJAX be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

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.

Does AJAX wait for response?

ajax() function with the default settings, any JavaScript code after this AJAX request will be executed without waiting for a response. In most cases, this is the desired behavior, but there are some situations where you will want to prevent further execution until there has been a response to the AJAX call.


1 Answers

If you want to handle the data coming back from the server before the event handler, use datafilter:

    jQuery.ajaxSetup({
        dataFilter: function (data, type) {
            //modify the data

            return data;
        }
    });
like image 112
Hugo Forte Avatar answered Nov 02 '22 19:11

Hugo Forte