【发布时间】:2016-08-06 21:52:09
【问题描述】:
我想使用 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。
谁能告诉我这段代码有什么问题?
【问题讨论】:
-
也许你可以直接说 headers: {'Authorization' : 'bearer ' + accessToken } 而不是在 beforeSend 中添加它们..
-
stackoverflow.com/questions/22140591/… 据我了解,如果没有拦截器,您不能在 angularjs "$http" 中使用 beforeSend。