【发布时间】:2018-10-08 13:32:01
【问题描述】:
我正在使用 ajax 来使用 Web 服务。
我的第一个请求是获取身份验证令牌的 POST。 (它正在工作。) 但第二个请求显示“401(未授权)”。 这可能是什么问题?
PS。我在 Chrome 上使用 CORS 扩展。如果我禁用它,我会得到“请求的资源上没有'Access-Control-Allow-Origin'标头”
当我在 Postman(禁用 SSL)上使用相同的令牌时,我的请求成功。 同样的令牌也适用于 python "requests.get(urlShow, headers=bpHeader, verify=False)"。
$.ajax({
type: 'POST',
url: 'url',
data: {"authType" : "password", "username" : "<username>", "password" : "<password>", "tenant" : "<tenant>"},
success: function(bpResponse){
var bpAuth = "Bearer " + bpResponse.token;
console.log(bpAuth);
$.ajax({
type: "GET",
url: 'url',
headers: {"Accept": "application/json", "Authorization": bpAuth},
success: function(bpResources){
console.log(bpResources);
}
});
}
});
这是我得到的控制台日志:
【问题讨论】:
-
您的网络服务似乎不接受您提供的令牌。这取决于服务的实现,您需要如何将令牌传递回 Web 服务。您使用的是什么网络服务?定制的?
-
使用选项
headers,而不是header