1

I would like to intercept and modify data from the server before the ajax success callback is executed

I have this code:

    jquery.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        // Modify options, control originalOptions, store jqXHR, etc
    });

and would like the equivalent:

    jquery.ajaxPostfilter(function (dataComingFromServer) {

        dataComingFromServer.applyMyModifications();
    });

ajaxPostfilter does not exist though - is there another way of intercepting the response from the server before it reaches event handler:

This question is related to this one - but it was not answered: How can I intercept ajax responses in jQuery before the event handler?

Community
  • 1
  • 1
Hugo Forte
  • 5,718
  • 5
  • 35
  • 44
  • 4
    Why ? What do you want to do in this filter that can't be done in the callback ? – Denys Séguret Jan 04 '13 at 20:47
  • 1
    why would you want that?? – Rafay Jan 04 '13 at 20:48
  • 2
    dataFilter should be able to do it. http://api.jquery.com/jquery.ajax/ Ctrl+F -> dataFilter (The new jQuery API design is up apparently) – Kevin B Jan 04 '13 at 20:48
  • If you want to intercept data before it gets to the event handler, use `preFilter`. What is `postFilter` supposed to do? – Asad Saeeduddin Jan 04 '13 at 20:51
  • 1
    @asad the preFilter happens before the request is sent. he wants to modify the data after it is received. This could be useful for converting invalid json into valid json if you can't control what the server is returning, or if you want to convert date strings into a different kind of date string before it reaches the success handler. – Kevin B Jan 04 '13 at 20:54
  • @KevinB So basically just an extra handler that fires before your regular success handler. Why not just do this processing in the `success` callback? – Asad Saeeduddin Jan 04 '13 at 21:01
  • 1
    @asad, exaction what KevinB said - I want to do this globaly, not in every regular handler. Thanks KevinB - I'll try the dataFilter - that looks like it'll do the trick. – Hugo Forte Jan 04 '13 at 21:03
  • @KevinB the success handler doesn't have the capability of modifying the data sent back from the server. – Hugo Forte Jan 04 '13 at 21:04
  • @HugoForte right, that's what the `dataFilter` I mentioned is for. – Kevin B Jan 04 '13 at 21:16
  • @KevinB - thanks again - that completely did the trick;-) – Hugo Forte Jan 04 '13 at 22:40

2 Answers2

1

If you want to use a standardised function to modify your data before the specific callback, you can use a pattern like this:

// Boilerplate to modify your data
function preprocess(data){
    data.foo = "bar";
    return data;
}

// Your specific handlers
function logdata(data){
    console.log(data);
}
function writedata(data){
    document.write(data);
}

// AJAX requests
$('#foo').load('url1', function(data) {
    logdata(preprocess(data));
});
$('#bar').load('url2', function(data) {
    writedata(preprocess(data));
});

Of course, this can be baked into a method that wraps the traditional .ajax, making it more convenient to use.

Note that the dataFilter option pointed out by KevinB serves a similar purpose, but it is limited to the .ajax method. If you wanted to set a global filter for AJAX requests, you would use:

$.ajaxSetup({
    dataFilter: preprocess
});
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1

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;
    }
});
Hugo Forte
  • 5,718
  • 5
  • 35
  • 44
  • You can use data filter directly from `$.ajax({ dataFilter : ... })` here's the doc's... https://api.jquery.com/jQuery.ajax/ – jaybeeuu Dec 10 '16 at 12:10