How to read response headers with $resource?
Asked Answered
C

1

7

I'm using $resource to get data from my RESTful service and I need to read response headers to get 'X-Page' and 'X-Total-Pages' value for pagination.

Example:

Access-Control-Max-Age:1728000
Cache-Control:max-age=0, private, must-revalidate
Connection:Keep-Alive
Content-Length:2637
Content-Type:application/json
Date:Thu, 10 Apr 2014 16:53:01 GMT
Server:WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
Vary:Origin
X-Page:1
X-Per-Page:10
X-Total:17
X-Total-Pages:2

But I couldn't get full headers from server.

This is returned headers: enter image description here

This is the headers from server: enter image description here

This is my code:

.factory('TestAPI', ['$resource',
        function ($resource) {
            return $resource("http://ip.jsontest.com/?callback=showIP", {}, {
                query: {
                    method: 'GET'
                }
            });
        }])

TestAPI.query({}, function (value, responseHeaders) {
                console.log(responseHeaders());
            }, function (response) {
                console.log(response);
 });
Coffeehouse answered 10/4, 2014 at 22:32 Comment(0)
W
11

In your response headers you have to add the following header:

Access-Control-Expose-Headers: X-Total-Pages, X-Page

With this, the browser is capable to expose your customs headers an read it angular.

Wiry answered 18/5, 2014 at 20:8 Comment(2)
How yoy get that please helppChampaign
I am unable to read X-AUTH-TOKEN from headers. Even after adding Access-Control-Expose-Headers: X-AUTH-TOKENDamara

© 2022 - 2024 — McMap. All rights reserved.