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.

Maniek Stasz
  • 347
  • 3
  • 13
  • You don't need `$q.when(response)`, doesn't make any sense here – Maxim Shoustin Nov 18 '13 at 13:27
  • did you check in headers tab in network profile of chrome whether server side is sending the headers or not – Ajay Beniwal Nov 18 '13 at 13:33
  • 1
    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? – Kos Prov Nov 18 '13 at 13:57
  • It is post request on another domain. Log: `Object {content-type: "text/html;charset=UTF-8"} ` – Maniek Stasz Nov 18 '13 at 15:14
  • If it is a [CORS](https://developer.mozilla.org/en/docs/HTTP/Access_control_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. – Kos Prov Nov 18 '13 at 15:59
  • It seems that I did something wrong with CORS requests. Now I make requests on the same domain and everything works OK. – Maniek Stasz Nov 18 '13 at 16:17
  • I did add `AuthToken` to `Access-Control-Allow-Headers` – Maniek Stasz Nov 18 '13 at 16:18

2 Answers2

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');
slamborne
  • 1,185
  • 11
  • 16
0

@slamborne, Is sounds good. For .Net

 <system.webServer>
  <httpProtocol>
      <customHeaders>
         <add name="Access-Control-Expose-Headers" value="AuthToken"/>
      </customHeaders>
    </httpProtocol>
 </system.webServer>
ITsDEv
  • 51
  • 3