jQuery and AJAX response header
cballou's solution will work if you are using an old version of jquery. In newer versions you can also try:
$.ajax({ type: 'POST', url:'url.do', data: formData, success: function(data, textStatus, request){ alert(request.getResponseHeader('some_header')); }, error: function (request, textStatus, errorThrown) { alert(request.getResponseHeader('some_header')); } });
According to docs the XMLHttpRequest object is available as of jQuery 1.4.
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 through 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.
var geturl; geturl = $.ajax({ type: "GET", url: 'http://....', success: function () { alert("done!"+ geturl.getAllResponseHeaders()); } });