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

AngularJS如何在Http请求中包含头

发布时间:2020-12-17 17:37:46 所属栏目:安全 来源:网络整理
导读:我是 angularjs的新手.我正在尝试发出需要授权的API请求.我已将它包含在请求的标题中,但它仍然无法正常工作.我确信我的访问令牌正在运行.有什么建议? $scope.fetch = function() { $scope.code = null; $scope.response = null; $http({ method: $scope.met
我是 angularjs的新手.我正在尝试发出需要授权的API请求.我已将它包含在请求的标题中,但它仍然无法正常工作.我确信我的访问令牌正在运行.有什么建议?

$scope.fetch = function() {
    $scope.code = null;
    $scope.response = null;
    $http({
        method: $scope.method,url: $scope.url,cache: $templateCache,headers: {
            Authorization: "access token"
        }
    }).
    success(function(data,status) {
        $scope.status = status;
        $scope.data = data;
    }).
    error(function(data,status) {
        $scope.data = data || "Request failed";
        $scope.status = status;
    });
};

解决方法

你可以使用以下

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";

它会将上述标题添加到您从应用程序发出的每个POST调用中.要添加所有方法共有的标头,请尝试以下操作.

$http.defaults.headers.common['Authorization'] = "Bearer " + user.oauthInfo.access_token;

(编辑:李大同)

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

    推荐文章
      热点阅读