user972255
user972255

Reputation: 1828

AngularJS - accessing http headers

I am trying to access the http headers in my angular controller but I am getting undefined. Also, I am able to see the header response in my angular service which is not reflecting in my controller. Can someone please tell me what I am missing? Please see the code below:

Service:

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

Controller:

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

Upvotes: 13

Views: 40789

Answers (3)

Amardeep Kohli
Amardeep Kohli

Reputation: 502

I had to access Token and TokenExpiry time from response headers of my Rest Service,then store it in my $rootScope. Here is the code I used:

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

Upvotes: 1

Jason
Jason

Reputation: 564

This question is old, but $http() returns a promise itself. you can just return that from your service, no need to create a new promise. You can do this even after using .success() and .error(), or for that matter even after using a .then(), they keep chaining.

Upvotes: 6

user972255
user972255

Reputation: 1828

I have found the solution by myself. All I have to do is create an array and add all those values to the same & return it to the controller. Please see the updated code below:

Service:

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

Controller:

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

Upvotes: 18

Related Questions