AngularJS如何在Http请求中包含头

前端之家收集整理的这篇文章主要介绍了AngularJS如何在Http请求中包含头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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;

猜你在找的Angularjs相关文章