AngularJS - 访问HTTP头

13

我想在我的 Angular 控制器中访问 HTTP 标头,但是我得到了未定义的结果。同时,我能够在我的 Angular 服务中看到标头响应,但在控制器中没有反映出来。请问我错过了什么?请查看下面的代码:

服务:

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
3个回答

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

工作得很好,谢谢信息。 - Ben Pretorius
在控制器中,应该像这样写:$scope.totalRecords = parseInt(response.headers("x-totalrowcount")); 这对我来说至少是有效的。 - John Ding

6

这个问题很旧了,但是$http()本身就返回一个promise。你可以直接从你的服务中返回它,不需要创建一个新的promise。即使在使用.success()和.error()之后,或者使用.then()之后,你也可以这样做,它们会继续链接。


1

我需要从我的Rest服务的响应头中访问TokenTokenExpiry时间,然后将其存储在我的$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');
                  });
                }

好方法!! - Sahil Dhir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接