質問

This is my sample, i'm not get response headers, it returns undefined I'm trying to get custom response headers like reponse.header['X-auth-token'] but it returns undefined

I'm new to angular js, Please 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 response in angularjs ? Please share your idea

Thanks in advance

役に立ちましたか?

解決

response.headers is a function an not a map, so you have to call it instead of accessing it via a key.

response.headers('headerName') should give you the respective header.

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

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

他のヒント

the first param of function is the returned data, the second param is headers; so you should write as follow:

checkStatus.query(function(data,headers) {
    console.log(headers('xxxx'));
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top