jQuery ajaxSetup(beforeSend 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19361908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajaxSetup (beforeSend not working
提问by abc123
after login to remote aPI server, and getting an access_token, I try to set the authorization header for all subsequent ajax calls :
登录到远程 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);
}
});
on console I can see :
在控制台上我可以看到:
GOT AUTHORIZATION login.js:34
Bearer 6b7578772fbb4178793100651f2234de840237fe
but none of subsequent ajax calls get the correct header set :
但随后的 ajax 调用都没有获得正确的标头集:
https://macmini.local:8000/Categories?_=1381758170726
cannot succeed as no access_token is found in the header ( server console ..)
无法成功,因为在标头中找不到 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
I tried to modify the header inside the ajax call wo any success
我试图修改 ajax 调用里面的标题 wo 是否成功
回答by abc123
Updated Answer
更新答案
As of jQuery 1.5 .ajax
supports headers
property
从 jQuery 1.5 开始.ajax
支持headers
属性
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
headers: {
'Authorization': authorization
}
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
}
});
Notes: the authorization
variable should be set somewhere before this call
注意:该authorization
变量应在此调用之前的某处设置
Old 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
工作,您需要在 document.ready 中调用它。
回答by Serhii Popov
I know this is an old question, but I came here from Google and thought other people searching the same thing should have a working solution. I have tried the suggested answer but it does not work for me.
我知道这是一个老问题,但我从 Google 来到这里,并认为其他搜索相同内容的人应该有一个可行的解决方案。我已经尝试了建议的答案,但它对我不起作用。
A solution which solved my problem is to set header directly in xhr
object inside of $.ajaxSend
:
解决我的问题的解决方案是直接在以下xhr
对象中设置标头$.ajaxSend
:
$(document).ajaxSend(function(ev, xhr, settings) {
xhr.setRequestHeader('Authorization', `Bearer ${localStorage.getItem(AUTH_TOKEN)}`);
xhr.setRequestHeader('X-Marketplace', localStorage.getItem('marketplace'));
});
You should call it in the document.ready
for the ajaxSend
to work.
你应该把它在document.ready
的ajaxSend
工作。
That similar questionhelped me.
那个类似的问题帮助了我。