Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Cannot read response headers from $http

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.

like image 623
GeoffreyB Avatar asked Mar 07 '15 20:03

GeoffreyB


2 Answers

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

like image 53
Brad Barber Avatar answered Sep 28 '22 01:09

Brad Barber


In success callback put :

console.log(headers('content-type'));
console.log(headers());  // All headers

I think the first line return a result in your cas.
In the seconde, have you got the 'Authentication' ?

The custom headers will be visible in same domain. In crossdomain case, you need to send Access-Control-Expose-Headers: Authentication, ... header from server.

like image 24
SilvR-O Avatar answered Sep 28 '22 00:09

SilvR-O