How to get headers from a response in angularjs

This is my example, I do not get response headers, it returns undefined I try to get custom response headers such as reponse.header ['X-auth-token'], but it returns undefined

I'm new to angular js, share your idea

Thanks in advance

// I'm trying to get custom response headers here (using alert (response.headers);)

controller

UIAppRoute.controller('test', ['$scope', 'checkStatus', function($scope, checkStatus) {
    $scope.data = {};
    checkStatus.query(function(response) {
        alert(response.headers);
        angular.forEach(response, function (item) {
             alert("resp2"+ item);
         });
      $scope.data.resp = response;
    });
}]);


// sending request to server
service
-------
UIAppResource.factory('checkStatus', function($resource){
    var auth = Base64.encode("abcde:abcde");
    return $resource(baseURL + "status", {},
        {
            'query': {
                method: 'GET',
                headers: {
                    'Accept':'application/json',
                    'Content-Type':'application/json',
                    'Authorization': 'Basic '+ auth,
                    'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept'
               },
                isArray: false
            }
        }
    )

how to get headers from a response in angularjs? Share your idea

Thanks in advance

+4
source share
2 answers

response.headers is a function, not a card, so you should call it instead of accessing it with a key.

response.headers('headerName') should provide you with an appropriate headline.

. http://code.angularjs.org/1.2.16/docs/api/ng/service/$http

$resource . http://code.angularjs.org/1.2.16/docs/api/ngResource/service/$resource

var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(u, headers){
  alert(headers('X-Internal-Auth-Token'))
  });
});
+5

- , - ; :

checkStatus.query(function(data,headers) {
    console.log(headers('xxxx'));
});
0

Source: https://habr.com/ru/post/1536554/


All Articles