将自定义访问令牌作为http请求发送

时间:2016-04-15 04:42:57

标签: angularjs ajax api oauth

我想使用oauth身份验证将api与mvc站点连接起来。 为此,我需要通过http请求发送访问令牌,如下所示。

addPostComment: function addPostComment(postid, UserId, comment, accessToken) {

                var request = $http({
                    method: "post",
                    url: apiPath.addPostComment,
                    data: {
                        SocialPostId: postid,
                        CommentDescription: comment,
                        CommentedUserId: UserId,
                        CommentedDate: new Date()
                    },
                    params: {
                        action: "post"
                    },                   
                    beforeSend: function (xhr) {

                        xhr.setRequestHeader('Authorization', 'bearer ' + accessToken);
                    }
                });
                return (request.then(handleSuccess, handleError));
            }

参数accessToken有值。但是当我设置为请求标头时,它总是说Authrization:Bearer undefined。

有人能告诉我这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

    $http({
      method: "post",
                url: apiPath.addPostComment,
                data: {
                    SocialPostId: postid,
                    CommentDescription: comment,
                    CommentedUserId: UserId,
                    CommentedDate: new Date()
                },
                params: {
                    action: "post"
                }, 
      headers: {
      'Authorization': 'bearer ' + accessToken
      }
    });

你可以像这样发送HTTP请求......