10

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.

2
  • 5
    You probably need to add Access-Control-Expose-Headers to your response from the server to make it accessible - see stackoverflow.com/questions/17038436/… Commented Mar 21, 2015 at 18:04
  • @BradBarber that's it. Reply if you want to get the bounty. Commented Mar 23, 2015 at 15:54

2 Answers 2

26
+50

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

Sign up to request clarification or add additional context in comments.

1 Comment

Will enabling CORS not impact on security. Shouldn't we send the required data in response body.
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.