登录到远程 API 服务器并获得 access_token 后,我尝试为所有后续 ajax 调用设置授权标头:
.done(function (result) {
console.log("GOT AUTHORIZATION");
amplify.store( "tokens", { access_token: result.access_token, refresh_token: result.refresh_token, token_type: result.token_type, expires_in: result.expires_in });
var authorization = 'Bearer ' + amplify.store( "tokens" ).access_token;
console.log(authorization);
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', authorization);
}
});
在控制台上我可以看到:
GOT AUTHORIZATION login.js:34
Bearer 6b7578772fbb4178793100651f2234de840237fe
但是后续的 ajax 调用都没有得到正确的标头集:
https://macmini.local:8000/Categories?_=1381758170726
无法成功,因为在标头(服务器控制台..)中找不到 access_token
{ code: 400,
error: 'invalid_request',
error_description: 'The access token was not found',stack: undefined }
saveAccessToken: 6b7578772fbb4178793100651f2234de840237fe, client_id: 1234567890, user_id: 1
我试图修改 ajax 调用中的标题 wo 任何成功