nope nope - 2 months ago 8
AngularJS Question

How can I read headers sent from my API with angular?

I have something similar to the following code on

domain.com
:

$http.post("http://api.domain.com/Controller/Method",
JSON.stringify(data),
{
headers: {
'Content-Type': 'application/json'
}
})
.then(function (response) {
console.log(response);
}, function (response) {
// something went wrong
});
}


It works great communicating with my .NET API.
response.data
has all of the data my server needs to give me. However we have a new security token that we are passing to the client from the API and we are passing it in back to the client in the packet header. I know that the token is being passed back because I can read it in the packet on the network tab in chrome debugger. However
response.headers()
only contains
content-type:"application/json; charset=utf-8"
It doesn't have what is in the packet. Any one have an idea?

The data is returned from the API like so (C#)
HttpContext.Current.Response.AppendHeader("session",Guid.NewGuid().ToString());


So i would expect
response
to have a header called
session
, but it does not. It is however in the packet.

Answer

Use the headers variable in success and error callbacks

$http.get('/url').
  success(function(data, status, headers, config) {
    //things to do
  })
  .error(function(data, status, headers, config) {
    //things to do
  });