2013-10-18 97 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

不错的做法! –