2014-02-27 167 views
22

这个简单的问题让我烦恼。我在Web Api Rest服务器的响应头中有一个自定义值。 我可以看到它Firebug为: X-TotalPages 204 我尝试在我的AngularJS控制器中获取它。下面的代码。 但我无法找到任何好例子如何做到这一点。 console.log(headers()['X-TotalPages']);日志 '不确定'

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

    }; 
+2

什么console.log(headers())的输出; ? – mafis

+2

请参阅http://stackoverflow.com/questions/17038436/reading-response-headers-when-using-http-of-angularjs – BKM

+1

感谢您的快速回复。的console.log(头());返回Headers:[object Object]所以它在那里。我已经看了另一个问题,但它没有帮助。这是我的回应,它似乎是由其他服务器返回:[IMG] http://i58.tinypic.com/5lrzft.png [/ IMG] –

回答

34

你应该使用:headers('X-TotalPages')

(发布Wawy的回答这样的问题可以得到解决。)

0

可以使用承诺的成功回调

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

    });