views:

1207

answers:

2

Hi people,

What i would like to do is send the return data from any ajax call and also have it avaiable in the ajaxComplete function.

So when i have a $.post or $.get or $.getJSON and so on request like this:

$.post(url, options, function(DATA)
{
    $('output').html(DATA);
});

i also want to get the same data into

$.ajaxComplete(function(event, XMLHttpRequest, ajaxOptions)
{
    // do something with DATA that is returned by the ajax request
    alert(DATA);
});

It means i want in the global ajaxComplete function also the same data which i get when i call one of the ajax functions.

Thanks for the replys.

+1  A: 

XMLHttpRequest.responseText

Note that if you set the dataType in the call to json or are using the getJSON function you will have to copy what jquery does internally and use eval( '(' + data + ')' ) to get the data into json so that it mirrors the data param passed to the success callback.

Or as activa points out just call the internal method to save the work.

redsquare
jQuery now provides a parseJSON method
SpoonMeiser
@SpoonMeiser it didnt on July 9th when I posted this!
redsquare
@redsquare No, I didn't mean to suggest it did. It was new in 1.4.1, and 1.3.2 appears to have been current when this question was posed. I just thought it worth making a note.
SpoonMeiser
+1  A: 

There's no easy way to do that, but you could extract the data from the xhr object. jQuery includes an internal function that does just that, but it's undocumented.

You can call this method like this:

$.ajaxComplete(function(event, xhr, options)
{
    var data = $.httpData(xhr,options.dataType);

    alert(data);
});

But beware: this is valid in jQuery 1.3.2, and because it's undocumented, it can change in future releases of jQuery.

Philippe Leybaert
++ to call the internal httpData
redsquare