0

Im trying to use jQuery.ajaxPrefilter API to make a HTTP Ajax interceptor.

Here is the jsFiddle

As you can see, I have access to the jqXHR object in the prefilter function, I can even console.log the entire jqXHR object, but some attributes, like responseJSON is returning undefined when pointing at them directly. This is the attribute I will like to modify in order to accomplish the HTTP interceptor.

Any ideas on why I cant access jqXHR.responseJSON attribute?

Here is the snippet of the code that is running on jsFiddle

var data = {
   'msg': 'hey!'
};

$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {

    console.log(jqXHR);   --> Returns the entire jqXHR object
    console.log(Object.isSealed(jqXHR));  --> return false
    console.log(Object.isFrozen(jqXHR));  --> return false
    console.log(jqXHR.responseJSON);      --> return undefined

});


$.ajax({
    type: 'POST',
    url: '/echo/json/',
    cache: false,
    data: {
        json: JSON.stringify(data),
        delay: 0
    }
}).done(function (data) {
    document.getElementById('msg').innerHTML = data.msg;
});

The rest of the code is working as intended, you can check it out on my fiddle.

franleplant
  • 629
  • 1
  • 7
  • 17
  • [`jqXHR`](http://jsapi.info/jquery/1.8.3/jQuery.ajax#L9347) is not the same as a normal [`XmlHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object – Andreas Dec 12 '13 at 14:00
  • `$.ajaxPreFilter()` gives you access to the request before it is send. With [`$.ajaxTransport()`](http://api.jquery.com/jQuery.ajaxTransport/) you can add a `complete` handler `function(status, statusText, responses, headers)` where `responses` _is an object containing dataType/value that contains the response in all the formats the transport could provide_ – Andreas Dec 12 '13 at 14:11
  • @Andreas thanks for your response. I know that jqXHR != XmlHttpRequest. – franleplant Dec 12 '13 at 19:13
  • @Andreas If you run my jsfiddle and inspect the console you will see that $.ajax.preFileter returns de jqXHR **reponse**, but lets not diverge from the problem. Why cant I access reponseJSON attribute given it is there, I can see it with the console.log? – franleplant Dec 12 '13 at 19:14
  • `responseJSON` is only available **after** the request has been sent and there is a response from the server - happens [here](http://jsapi.info/jquery/1.8.3/jQuery.ajax#L9433) in the source. As already said `$.ajaxPreFilter()` happens **before** the request is send. [fiddle](http://jsfiddle.net/fZ7jA/) – Andreas Dec 13 '13 at 07:43
  • Nice Site! I understand what you are saying, it make sense. But if you look at my fiddle or this modified [fiddle](http://jsfiddle.net/fZ7jA/1/) you will see that in line 7 the jqXHR displays in the console with the responseJSON attribute not undefined, but in line 8, the jqXHR.responseJSON display as undefined, my question points out to why? that happens? Make sense? – franleplant Dec 14 '13 at 17:01
  • Have a look at [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays). As a prove of the lazy evaluation just add a delay to the ajax request and you should get the correct result (no `responseXXX` property in `jqXHR` in `.ajaxPreFilter()`) - [fiddle](http://jsfiddle.net/fZ7jA/2/) – Andreas Dec 15 '13 at 08:03
  • Thank you very much! that explains everything! – franleplant Dec 16 '13 at 13:04

0 Answers0