本文介绍了ajaxSetup(beforeSend不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
登录到远程API服务器,并得到一个access_token后,我尝试设置授权头的所有后续Ajax调用:
.done(函数(结果){
的console.log(得到授权);
amplify.store(令牌,{access_token:result.access_token,refresh_token:result.refresh_token,token_type:result.token_type,expires_in:result.expires_in});
VAR授权='承载'+ amplify.store(标记).access_token;
执行console.log(授权);
$ .ajaxSetup({
beforeSend:功能(XHR){
xhr.setRequestHeader(授权,授权);
}
});
在控制台上,我可以看到:
GOT授权login.js:34
承载6b7578772fbb4178793100651f2234de840237fe
但没有随后的AJAX调用得到正确的报头组:
HTTPS://macmini.local:8000 /分类_ = 1381758170726?
因为没有access_token在头文件中不能成功(服务器控制台..)
{code:400,
错误:INVALID_REQUEST,
error_description:访问令牌未找到,堆栈:未定义}
saveAccessToken:6b7578772fbb4178793100651f2234de840237fe,CLIENT_ID:1234567890,USER_ID:1
我试图修改Ajax调用内部的头を任何成功
解决方案
答案
$(函数(){
$ .ajaxSetup({
beforeSend:功能(XHR){
xhr.setRequestHeader(授权,授权);
}
});
});
对于 ajaxSetup
来工作,你需要调用它的的document.ready。
after login to remote aPI server, and getting an access_token, I try to set the authorization header for all subsequent ajax calls :
.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);
}
});
on console I can see :
GOT AUTHORIZATION login.js:34
Bearer 6b7578772fbb4178793100651f2234de840237fe
but none of subsequent ajax calls get the correct header set :
https://macmini.local:8000/Categories?_=1381758170726
cannot succeed as no access_token is found in the header ( server console ..)
{ code: 400,
error: 'invalid_request',
error_description: 'The access token was not found',stack: undefined }
saveAccessToken: 6b7578772fbb4178793100651f2234de840237fe, client_id: 1234567890, user_id: 1
I tried to modify the header inside the ajax call wo any success
解决方案
Answer
$(function(){
$.ajaxSetup({
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', authorization);
}
});
});
For the ajaxSetup
to work you need to call it in the document.ready.
这篇关于ajaxSetup(beforeSend不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!