23

This simple problem is bugging me. I have a custom value in my response header from the Web Api Rest server. I can see it it Firebug as: X-TotalPages 204 I try to get it in my AngularJS controller. Code below. But I cant find any good examples how to do this. console.log(headers()['X-TotalPages']); logs 'undefined'

var assets = angular.module("Assets", ['ui.bootstrap']);
assets.controller('AssetSearchController', function ($scope, $http) {
    $scope.test = 'very';
    $scope.getItems = function () {
        $http({ method: 'GET', url: 'http://localhost:57772/api/assets', params: { search: 1, page: 0, pageSize: 10 } }
            ).success(function (data, status, headers, config) {
                $scope.currentPage = 4;                
                console.log(headers()['X-TotalPages']);

            $scope.items = data;
        }).
        error(function (data, status) {
            console.log(JSON.stringify(data));
            console.log(JSON.stringify(status));
        });

    };
7
  • 2
    What's the output of console.log(headers()); ? Commented Feb 27, 2014 at 9:37
  • 2
    Please see stackoverflow.com/questions/17038436/… Commented Feb 27, 2014 at 9:44
  • 1
    Thanks for the quick reply. console.log(headers()); returns Headers: [object Object] so its there. I have already looked at the other issue and it does not help. This is my response, it seems to be returned by the rest server :[IMG]i58.tinypic.com/5lrzft.png[/IMG] Commented Feb 27, 2014 at 9:54
  • 2
    And my web-config in Rest service:<httpProtocol> <customHeaders> <add name ="Access-Control-Expose-Headers" value="X-TotalPages"/> <add name="Access-Control-Allow-Origin" value="" /> <add name="Access-Control-Allow-Methods" value="POST,GET,DELETE,PATCH,PUT,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="" /> </customHeaders> </httpProtocol> Commented Feb 27, 2014 at 9:57
  • 8
    Shouldn't it be headers('X-TotalPages')? Commented Feb 27, 2014 at 10:03

3 Answers 3

38

You should use: headers('X-TotalPages')

(Posting Wawy's answer so the question can be resolved.)

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

Comments

6

For $http(url).then() syntax which replaced $http(url).success().error() in newer versions of AngularJS, I used this:

$http(url).then(function(response){
            response.headers("X-TotalPages");
     });

Just using response.headers() will give all headers attached in response.

Comments

0

You can use success callback of promise

promise.then(function(resp){
            resp.headers().token(variablename)

     });

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.