AngularJS - Cannot read response headers from $http

GeoffreyB picture GeoffreyB · Mar 7, 2015 · Viewed 17.8k times · Source

My http response contains the headers Authentication (as mentioned here: Authentication:76efbc0946773b62c93e952b502a47acd898200f6f80dc46ac87ffc501c00780) when I inspect the request with the inspector, but a call to headers("Authentication") returns null

return $http({
    method: "GET",
    url: url,
    headers: {
      'Content-Type': "application/json"
    }
}).success(function (data, status, headers, config) {
    console.log(headers("Authentication"));
})

Do you have any idea about what I could be doing the wrong way ?

FYI, i've tried to switch it back to a "promise" way, with .then, and the issue is still the same.

Answer

Brad Barber picture Brad Barber · Mar 24, 2015

Your code looks good but if it's a CORS request the server needs to include Access-Control-Expose-Headers: {any custom headers} in the response.

There's a previous question/answer with more details: Reading response headers when using $http of Angularjs