64

I am using $http to make an api call which is sending some custom header like X-Foo. However I can't yet figure out how to read them. Inside the $http({...}).success(function (data, status, headers, config) {...}) function, headers is a function that should give me a hash of all headers but it only shows the header content-type. Is there a way to get the response headers ?

7
  • Are you using Firefox? See stackoverflow.com/questions/14188662/…. Commented Jun 11, 2013 at 7:31
  • No, chrome. Does the FF specific fix work for chrome ? Commented Jun 11, 2013 at 15:43
  • 2
    Are the requests being made to a different domain or the same domain? I tested sending the request to the same domain and I am able to see the custom headers. Commented Jun 12, 2013 at 4:17
  • 2
    You are right, the issue was due to different domains. I could finally get the custom headers to show after I made the server to send the header Access-Control-Expose-Headers. Commented Jun 12, 2013 at 17:58
  • 18
    @JoyDutta You should create an answer, so to make this question not listed under "unanswered" tab. Commented Jul 11, 2013 at 20:37

2 Answers 2

149

The custom headers will be visible in same domain. However, for the crossdomain situation, the server has to send Access-Control-Expose-Headers: X-Foo, ... header to make the custom headers visible.

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

5 Comments

It appears that you must specify the actual header name instead of a wildcard. Access-Control-Expose-Headers:* does not work, but Access-Control-Expose-Headers:Etag does.
this did not seem to work for me with angular's $http
Seems that even standard headers don't work with CORS without this workaround. Had to add this: Access-Control-Expose-Headers: Content-Disposition to make the content-disposition header visible in angularjs.
This is how to do it on a rails backend: jaketrent.com/post/expose-http-headers-in-cors
You should let angular handle the cookies. You'll need to do this on the servlet side to allow the other domain to set cookies: ` resp.setHeader("Access-Control-Allow-Credentials", "true");`
0

Spring 4.0+ provide @CrossOrigin annotation which has following parameters

  1. origins = list of Comma separated origin.
  2. exposedHeaders = list ofcomma separated count custom parameters.

example

@CrossOrigin(origins = "*", exposedHeaders ="X-Total-Count")

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.