2013-10-18 102 views
13

我想訪問我的角度控制器中的http頭,但我越來越未定義。另外,我能夠看到我的角度服務中的頭部響應,這並不反映在我的控制器中。有人能告訴我我錯過了什麼嗎?請參閱下面的代碼:AngularJS - 訪問http頭

服務:

cmApp.service('supplierService', function ($http, $q) { 
    this.getSuppliers = function (orderByColumn, skipRows, takeRows) { 
     var deferred = $q.defer(); 
     $http({ 
      method: 'GET', 
      url: 'api/supplier', 
      params: { orderBy: orderByColumn, skip: skipRows, take: takeRows }, 
      timeout: 30000, 
      cache: false 
     }). 
     success(function (data, status, headers, config) { 
      // any required additional processing here    
      deferred.resolve(data, status, headers, config);    
     }). 
     error(function (data, status) { 
      deferred.reject(data, status, headers, config); 
     }); 
     return deferred.promise;   
    } 

控制器:

supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take) 
     .then(function (data, status, headers, config) { 
      **//getting undefined here.** 
      $scope.totalRecords = parseInt(headers('X-TotalRowCount'));     
      $scope.suppliers = data; 
     }, function (error) { 
      // error handling here 
     }); 
+1

請看$ httpProvider。用它來控制你的頭。 – kroonwijk

回答

18

我發現我自己的解決方案。我所要做的就是創建一個數組並將所有這些值添加到同一個&將其返回給控制器。請參閱下面的更新的代碼:

服務:

cmApp.service('supplierService', function ($http, $q) { 
    this.getSuppliers = function (orderByColumn, skipRows, takeRows) { 
     var deferred = $q.defer(); 
     $http({ 
      method: 'GET', 
      url: 'api/supplier', 
      params: { orderBy: orderByColumn, skip: skipRows, take: takeRows }, 
      timeout: 30000, 
      cache: false 
     }). 
     success(function (data, status, headers, config) { 
      // any required additional processing here 
      var results = []; 
      results.data = data; 
      results.headers = headers(); 
      results.status = status; 
      results.config = config; 

      deferred.resolve(results);    
     }). 
     error(function (data, status) { 
      deferred.reject(data, status, headers, config); 
     }); 
     return deferred.promise;   
    } 

控制器:

supplierService.getSuppliers($scope.orderby, $scope.skip, $scope.take) 
      .then(function (response) {     
       $scope.suppliers = response.data; 
       $scope.totalRecords = parseInt(response.headers["x-totalrowcount"]);     
      }, function (error) { 
       // error handling here 
      }); 
+0

工作的很好,非常感謝你的信息 –

+0

非常感謝你。幫了很多忙。 – victorkurauchi

+0

在控制器中,應該是這樣的:$ scope.totalRecords = parseInt(response.headers(「x-totalrowcount」));這項工作對我來說至關重要。 –

6

這個問題是舊的,但$ HTTP()返回一個承諾本身。您只需從您的服務中退還,無需創建新的承諾。甚至在使用.success()和.error()之後,即使在使用.then()之後,您也可以執行此操作,但它們會保持鏈接。

2

自定義標題將在同一個域中可見。但是,對於跨域情況,服務器必須發送Access-Control-Expose-Headers:X-Foo,並將跨域屬性設爲* ...標題,以使自定義標題可見。

1

我不得不響應訪問令牌TokenExpiry時間頭我休息服務的,然後將其存儲在我的$ rootScope。 這裏是我使用的代碼:

   $scope.Authenticate=function(){ 
        var EncDecUserPass=decodeURIComponent(encodeURIComponent($scope.LoggedUserName+':'+$scope.LoggedUserPassword)) ; 
        $http(
        {method: 'GET', 
        url: 'http://localhost:53256/api/Products/Authenticate', 
        cache: false, 
        headers:{'Authorization':'Basic '+window.btoa(EncDecUserPass)} 
        } 
        ).success(function(data, status, headers, config) { 
         //Here it goes 
         $rootScope.token=headers().token; 
         $rootScope.tokenExpirySec=headers().tokenexpiry; 
        }).error(function(data, status, headers, config) { 
        alert('Invalid User'); 
        }); 
       } 
+0

不錯的做法! –