加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何从angularjs中的响应中获取标头

发布时间:2020-12-17 17:18:55 所属栏目:安全 来源:网络整理
导读:这是我的样本,我没有得到响应头,它返回undefined 我正在尝试获取自定义响应标头,如reponse.header [‘X-auth-token’],但它返回undefined 我是棱角分明的新手,请分享你的想法 提前致谢 //我在这里尝试获取自定义响应标头(使用alert(response.headers);) 调节
这是我的样本,我没有得到响应头,它返回undefined
我正在尝试获取自定义响应标头,如reponse.header [‘X-auth-token’],但它返回undefined

我是棱角分明的新手,请分享你的想法

提前致谢

//我在这里尝试获取自定义响应标头(使用alert(response.headers);)

调节器

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

如何从angularjs中的响应中获取标题?
请分享你的想法

提前致谢

解决方法

response.headers是一个函数而不是一个map,所以你必须调用它而不是通过一个键来访问它.

response.headers(‘headerName’)应该为你提供相应的标题.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读