$http response interceptors headers
Asked Answered
O

2

11

I'm sending a custom header from server in a response. In $http response interceptor I want to get this header, but the only header I could get is Content-type header. How can I resolve my problem?

Piece of my $http interceptor:

response: function (response) {
            var AuthToken = response.headers('AuthToken');
            return response || $q.when(response);
        },

AuthToken is undefined.

Olivero answered 18/11, 2013 at 13:12 Comment(7)
You don't need $q.when(response), doesn't make any sense hereSquiggle
did you check in headers tab in network profile of chrome whether server side is sending the headers or notCathiecathleen
Angular's response header handling seems pretty good to me. It even fixes a getAllResponseHeaders() bug in firefox. Try console.log(response.headers()) to see the whole collection. BTW, is this a JSONP request on another domain?Ketcham
It is post request on another domain. Log: Object {content-type: "text/html;charset=UTF-8"} Olivero
If it is a CORS request you should configure the target server to also return Access-Control-Allow-Headers to instruct the browser that JavaScript from the other domain is allowed to read certain headers.Ketcham
It seems that I did something wrong with CORS requests. Now I make requests on the same domain and everything works OK.Olivero
I did add AuthToken to Access-Control-Allow-HeadersOlivero
J
29

This is a CORS issue.

The response should include Access-Control-Expose-Headers listing the specific headers you'd like to use.

e.g. Access-Control-Expose-Headers: AuthToken, AnotherCustomHeader

Depending on your server setup, this could be set site wide using an .htaccess file (Apache)

<IfModule mod_headers.c>
Header set Access-Control-Expose-Headers AuthToken,AnotherCustomHeader
</IfModule>

Or set per request in your server code (php)

header('Access-Control-Expose-Headers: AuthToken, AnotherCustomHeader');
Jasminejason answered 26/1, 2014 at 10:12 Comment(2)
Thank you. So surprising to see it in the net panel but not in angular code. Makes sense now.Danielladanielle
@YaronU. I try. Well...sometimes... :PJasminejason
O
0

@slamborne, Is sounds good. For .Net

 <system.webServer>
  <httpProtocol>
      <customHeaders>
         <add name="Access-Control-Expose-Headers" value="AuthToken"/>
      </customHeaders>
    </httpProtocol>
 </system.webServer>
Oday answered 16/12, 2016 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.