AngularJS $http response header

Jens Alenius picture Jens Alenius · Feb 27, 2014 · Viewed 54.1k times · Source

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));
        });

    };

Answer

barsju picture barsju · Jun 20, 2014

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

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