我正在尝试通过使用电子邮件ID和角色创建用户角色,在标题中我传递内容类型和授权,当我提交表单时,我得到的XMLHttpRequest无法加载错误。如何在angualr js中传递授权令牌。
var Access_Token = L5uCp_JwSa9Fk7_ShrqPWi1FPdT_.......
$http({
url: "http://localhost:54581/api/Account/AddRole?username=" + email + "&Role=User",
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'bearer ' + Access_Token
}
}).
then(function (response) {
alert("Success")
})
答案 0 :(得分:0)
如果您使用的是POST方法,则无法直接将params附加到网址。您需要首先使用angular内置的httpParamSerializer对其进行序列化,然后将其发送到数据中。
var myData = {username : email,Role : "user"}
$http({
url: http://localhost:54581/api/Account/AddRole,
method: 'POST',
data: $httpParamSerializerJQLike(myData),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'bearer ' + Access_Token
}
});