16

I have a a form that I am submitting via ajax. I am using the jquery form plugin. What I am trying to do is get the 'Location' header which is returned from my server. I can see it in firebug. But whenever I call the getResponseHeader() function in my success callback, it always returns 'undefined'..

Code:

form.ajaxForm({ dataType: 'xml', data: {format: 'xml'}, resetForm: true, success: function(xml,status,xhr){ var location = xhr.getResponseHeader('Location'); alert(location); }); 

location is undefined. But I can see the 'Location' header in firebug. What am I missing? Even if I call getAllResponseHeaders() from the xhr object, it returns 'undefined'

1
  • Just to add: This has been tested in both firefox and opera. Same results Commented Dec 6, 2010 at 19:22

3 Answers 3

38

If this is a CORS request, you may see all headers in debug tools (such as Chrome->Inspect Element->Network), but the xHR object will only retrieve the header (via xhr.getResponseHeader('Header')) if such a header is a simple response header:

  • Content-Type
  • Last-modified
  • Content-Language
  • Cache-Control
  • Expires
  • Pragma

If it is not in this set, it must be present in the Access-Control-Expose-Headers header returned by the server.

About the case in question, if it is a CORS request, one will only be able to retrieve the Location header throgh the XMLHttpRequest object if, and only if, the header below is also present:

Access-Control-Expose-Headers: Location 

If its not a CORS request, XMLHttpRequest will have no problem retrieving it.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, if you have to use CORS and have these problems.
6

An XMLHttpRequest will transparently follow a redirect, so the final request won't have the header, it's already followed that redirect and you're seeing the response headers from that request (not the initial request which had the Location header).

10 Comments

The server generates a 201 Created response. I sure hope jquery doesn't interpret this as a redirect. And firebug does not show a second request.
@Nik - It has a Location header though, right? that's a redirect.
It does have a location header, but if I understand the http response codes, 30x series means redirect. not 20x. 20x does not imply further action. The location header should be informational. But maybe I am wrong?
@Nik - Do you have a server I can see this on? You're sure the initial response is a 201, the initial one with the Location header...not the second?
After checking my server logs for my ruby on rails application, I do not see a second request after the 201 response. So xhr is not redirecting to the location. That is fine, but maybe jquery is getting confused? Any more ideas? Thanks for your help. ;)
|
4

I'm doing something similar using the rails/rest way of returning a 201 "created" with a Location header to the new object and an empty body. jQuery's ajax method will throw a "parseerror" when encountering this since its expecting json but getting nothing back. I simply catch the 201 redirect in my error callback like so:

function request_error(req, textStatus, errorThrown) { if (req.status == 201 ) { var created_loc = req.getResponseHeader('Location'); console.log('(201) created: ' + created_loc); // ... manual redirect here i.e. issue another ajax request to created_loc return; } // ... handle an actual error here } 

hope this helps!

1 Comment

Just set dataType: 'text' as arg, and you can catch the 201 in the complete callback (or even better: jqXHR.done).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.