13

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
        });
1
  • 1
    Please look into $httpProvider. Use that to control your header. Commented Oct 18, 2013 at 16:52

3 Answers 3

18

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
            });
Sign up to request clarification or add additional context in comments.

1 Comment

In controller, should be like this:$scope.totalRecords = parseInt(response.headers("x-totalrowcount")); this work for me at lease.
6

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.

Comments

1

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

1 Comment

nice approach !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.