37

I'm having a problem with AngularJS's $http service not returning all of the headers from the API I'm polling. Here's what I've got:

$http({
    method: 'POST',
    withCredentials: true,
    url: 'http://api.mydomain.com/query',
    data: JSON.stringify(parameters)
})
.success(function(data, status, headers, config){
    ... // setting some scope parameters based on data
    console.log(headers());
})

I can tell through the network tab in Chrome that a bunch of response headers are being returned by the API (I'm particularly interested in the X-Pagination-Total-Items header).

Here's a screenshot of the network tab from the request: Chrome's Network Panel

But the console.log statement above (which should output all headers) only returns two: Console Log...missing headers!

Any idea what's going on? How do I actually access all of the headers coming back from the AJAX call?

Thanks,

-Nate

2
  • Which headers can't you access? (Also, consider prepending them with X-) Commented Feb 7, 2014 at 18:59
  • Benjamin, I'm trying to get X-Pagination-Total-Items. I'm wondering if it's a CORS issue and whether Access-Control-Expose-Headers can't take a wildcard... Commented Feb 7, 2014 at 19:05

1 Answer 1

61

Turns out that the Access-Control-Expose-Headers cannot accept a wildcard. We needed to specify exactly which headers the client should have access to, and then it worked.

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

8 Comments

I spent HOURS trying to figure this out. Thank you, thank you, a million times thank you
@EliGassert, glad it helped you as well. One of the great things about StackOverflow! :-)
Oh my god.. I too forgot to add these headers to my CORS filter. No wonder I could see them in the browser response but not in the AngularJS headers() call output.
Is this on the backend issue?
Yes, this is a backend issue. You should configure your web server to send back desired header fields. Wildcards are not supported so this won't work; Access-Control-Expose-Headers:* Instead you should provide exact header field i.e. Access-Control-Expose-Headers:Etag
|

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.