3

I would like to get the 'X-Total-Count' response header of a RESTful API. While trying to get the header in the query callback function of my ngResource 'User', it seems that $http ignores a lot of the reponse headers.

These are the response headers of my request:

Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Connection: close
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Fri, 17 Oct 2014 11:13:26 GMT
Link: <http://xxxx.xxx/user?page=2>; rel="next"
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Total-Count: 32

Here I'm querying the set of users:

User.query({
    page: $scope.pagingOptions.currentPage, 
    limit: $scope.pagingOptions.pageSize
}, function(users, responseHeaders) {
    console.log(responseHeaders());
    console.log(responseHeaders('X-Total-Count'));
});

That's the result of the console:

Object { cache-control="max-age=0, private, must-revalidate", content-type="application/json; charset=utf-8"}
null

So why the $http's responseHeaders() function turns only 2 of 10 header properties?

MonsJovis
  • 186
  • 1
  • 8
  • 4
    You also need `Access-Control-Expose-Headers: X-Foo` as described here: http://stackoverflow.com/questions/17038436/reading-response-headers-when-using-http-of-angularjs – Sergiu Paraschiv Oct 17 '14 at 11:41
  • I have exactly the same problem, using a $http.get().success() in my case, even with adding the `Access-Control-Expose-Headers: X-Total-Count` in my API service response, i'm unable to get my header in angular ... Have you found any solution so far ? – Sylver Dec 08 '14 at 10:42

1 Answers1

0

Are you sure you are not doing cross origin resource sharing?

see 7.1.1 Handling a Response to a Cross-Origin Request

That specification forbids access to any response header field other except the simple response header fields (i.e. Cache-Control, Content-Type, ...)

Luca
  • 4,223
  • 1
  • 21
  • 24