21

So I am trying to get the location from a header response via jQuery get. I tried using getResponseHeader('Location') and getAllResponseHeaders() but they both seem to return null.

Here's my current code

$(document).ready(function(){
   var geturl;
   geturl = $.ajax({
      type: "GET",
      url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)',
   });
   var locationResponse = geturl.getResponseHeader('Location');
   console.log(locationResponse);
});
raeq
  • 971
  • 4
  • 15
  • 36
  • probably duplicate of http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – Naren Sisodiya Jun 27 '12 at 10:28
  • Possible duplicate of [jQuery and AJAX response header](https://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header) – user2226755 Mar 30 '19 at 07:32
  • And duplicate of [How to get the final URL after an Ajax redirect?](https://stackoverflow.com/questions/30993442/how-to-get-the-final-url-after-an-ajax-redirect) – cachius Mar 23 '23 at 09:15

3 Answers3

34

The headers will be available when the asynchronous request returns, so you will need to read them in the success callback:

$.ajax({
    type: "GET",
    url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)',
    success: function(data, status, xhr) {
        console.log(xhr.getResponseHeader('Location'));
    }
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    tried that with no luck. The url is not returning a success statement. I tried printing it out on false as well with no luck – raeq Jun 27 '12 at 11:16
  • 3
    try a `complete: function(xhr){ console.log(xhr.getAllResponseHeaders()); }` – Bergi Jun 27 '12 at 11:20
  • 3
    The error you get there is probably because of the Same-Origin-Policy. – Bergi Jun 27 '12 at 17:02
  • 6
    @raeq this helped me: http://stackoverflow.com/questions/5822985/cross-domain-resource-sharing-get-refused-to-get-unsafe-header-etag-from-re – Philipp Kyeck Feb 22 '13 at 10:40
  • 1
    success method calls only if response status is 200, but in this case I think it is 302 – Sameera Kumarasingha Feb 20 '17 at 16:01
  • I don't know if it's changed in the last 7 years, but this no longer works and I don't think it ever should have. Ajax follows redirects, so you would not have a "Location" header because that would create a redirect that would be followed, and you would be returned the resulting page instead. For info see: https://stackoverflow.com/questions/8238727/how-to-prevent-ajax-requests-to-follow-redirects-using-jquery – Radley Sustaire Jun 19 '19 at 05:54
  • @RadleySustaire IIRC only 3xx statuses are followed, OP might get the location header in a 200 response. But I guess when writing the answer, I didn't even look at that part, and only solved the asynchrony issue that the OP had. – Bergi Jun 19 '19 at 10:26
8

for some headers in jQuery Ajax you need to access XMLHttpRequest object

var xhr;
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
  xhr = _orgAjax();
  return xhr;
};

$.ajax({
    type: "GET",
    url: 'http://example.com/redirect',
    success: function(data) {
        console.log(xhr.responseURL);
    }
});

or using plain javascript

var xhr = new XMLHttpRequest();
xhr.open('GET', "http://example.com/redirect", true);

xhr.onreadystatechange = function () {
  if (this.readyState == 4 && this.status == 200) {
    console.log(xhr.responseURL);
  }
};

xhr.send();
uingtea
  • 6,002
  • 2
  • 26
  • 40
  • 1
    This is not a cross-browser compatible solution. IE does not support the responseURL property (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseURL) and as a result will always be undefined in IE. – SunshinyDoyle Dec 18 '18 at 20:13
  • @SunshinyDoyle It's important to tell that this itself is _impossible in IE_. You made it sound like there might be another way, but there is none. This is as good as it gets. – cachius Mar 23 '23 at 15:31
7

jQuery abstracts the XMLHttpRequest object in a so-called "super set" that does not expose the responseURL field. It's in their docs where they talk about the "jQuery XMLHttpRequest (jqXHR) object"

For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:

readyState
responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
status
statusText
abort( [ statusText ] )
getAllResponseHeaders() as a string
getResponseHeader( name )
overrideMimeType( mimeType )
setRequestHeader( name, value ) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
statusCode( callbacksByStatusCode )
No onreadystatechange mechanism is provided, however, since done, fail, always, and statusCode cover all conceivable requirements.

As you can see there is no way to get hold of the response URL because the jqXHR API does not expose it

Glen van Ginkel
  • 126
  • 1
  • 2
  • 2
    Seemingly the only answer on the page which holds any form of correctness, jQuery doesn't expose responseUrl nor does it provide Location within the response headers. – N.J.Dawson Apr 18 '18 at 12:13