2014-02-27 164 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) 

    });