jquery: unable to print ajax response header (Jquery 1.7.1)

1.6k Views Asked by At
  $.ajax({
                async:false,
                type: 'POST',
                url: itemURL,
                success: function(data,status,jqXHR) {
                    responseObj  = data;
                    console.log('success function resp');
                    console.log(jqXHR.getAllResponseHeaders());
                },
                error: function(data){
                    responseObj = data;
                },
                data:item,
                dataType: "json",

    });

Here's my code; i am unable to print response headers; am i missing anything? all that prints out is empty string.

Tried using getResponseHeader("Location"), that's not working either; I am trying to get "Location" header that's being returned for the AJAX call.

However firbeug shows all response headers including "Location" which I am after.

I am using Jquery 1.7.1

1

There are 1 best solutions below

0
On BEST ANSWER

I worked with @Satish in helping to answer this question. This was basically a CORS issue, but, it turns out there were two issues involved here:

1) The server needed to add 'Location' to the Access-Control-Expose-Header response header, this allows conforming XHR Level-2 clients to see the extra header.

2) WebKit clients had a bug where they would ignore the Access-Control-Expose-Header when returning headers for an XHR response. This was fixed in WebKit recently https://bugs.webkit.org/show_bug.cgi?id=76419, and we verified that it's now working in Safari based on that WebKit.

Just to add some background, the request was POSTing to a REST service to create an object. The server response had a status of 201 and stored the location of the new resource in the 'Location' header. As this was a CORS request, the XHR was stripping the 'Location' header. Adding the header and upgrading to a version of WebKit with the fix corrected this issue.