How to get Response Header location from jQuery Get?

raeq picture raeq · Jun 27, 2012 · Viewed 68.3k times · Source

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);
});

Answer

Bergi picture Bergi · Jun 27, 2012

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'));
    }
});