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

3 に答える 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
            });
于 2013-10-18T17:02:50.367 に答える
1

Rest Service の応答ヘッダーからTokenおよび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');
                  });
                }
于 2017-08-21T05:45:02.440 に答える