在 Chrome/Safari/Webkit 浏览器上未使用 jQuery.ajax() 的 headers 参数设置 HTTP 请求标头
我正在使用 jQuery 1.6.2,并且使用 jquery.ajax()
方法。我正在使用此参数进行 HTTP 基本身份验证。该请求在 Firefox 中运行良好,并且已从服务器成功检索数据。但是,在 Chrome 和 Safari 中,我收到输入用户名和密码的提示。查看 Chrome 中的 HTTP 请求,我没有看到 Authorization
请求标头。然而,在 Firefox 上,我是这么做的。
您知道这里发生了什么以及如何解决这个问题吗?
更新
这是所要求的代码。我尝试过使用 1.5 之前和之后的方法来设置 HTTP 请求标头,但没有成功。
$(function(){
$.ajax({
url: "<some url>",
dataType: "jsonp",
// jquery 1.5+ Approach
// headers: {
// "Authorization":"Basic username:password",
// "Content-Type":"application/x-www-form-urlencoded"
// },
// jQuery 1.4 Approach
// beforeSend: function(jqXHR, settings) {
// jqXHR.setRequestHeader("Authorization", "Basic username:password");
// jqXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// },
success: function(data){
// do stuff
}
});
});
I am using jQuery 1.6.2 and I am seeing odd behaviour using the headers
parameter of the jquery.ajax()
method. I am using this parameter to do HTTP Basic Authentication. The request works fine in Firefox and data is retrieved successfully from a server. However, in Chrome and Safari, I get a prompt to enter a username and password. Looking at the HTTP Request in Chrome, I do not see the Authorization
request header. However, on Firefox, I do.
Any ideas what's going on here and how to remedy this issue?
Update
Here is the code as requested. I've tried using both the pre- and post-1.5 way of setting HTTP Request Headers to no avail.
$(function(){
$.ajax({
url: "<some url>",
dataType: "jsonp",
// jquery 1.5+ Approach
// headers: {
// "Authorization":"Basic username:password",
// "Content-Type":"application/x-www-form-urlencoded"
// },
// jQuery 1.4 Approach
// beforeSend: function(jqXHR, settings) {
// jqXHR.setRequestHeader("Authorization", "Basic username:password");
// jqXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// },
success: function(data){
// do stuff
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您为
Content-Type
标头提供什么值?如果您没有指定它,请尝试以下标头。What value are you providing for your
Content-Type
header? If you aren't specifying it, try the following header.我找到了一个对我有用的答案。取自
带有 HTTP 授权的 Chrome 扩展中的 XMLHttpRequest 继续在常规页面上发送授权标头
=>
在授权标头中附加一个额外的“:x”,如下所示:
xmlReq.setRequestHeader("Authorization", "Basic "+base64_encode(access_token+":x"));
适用于 FF 和 Chrome。在 IE9 中不起作用 - 我仍在对此进行调查。
华泰
托马斯
I found an answer that worked for me. Taken from
XMLHttpRequest in Chrome Extension with HTTP Authorization continues to send Authorization Header on regular pages
=>
Append an extra ":x" to your Authorization header like this:
xmlReq.setRequestHeader("Authorization", "Basic "+base64_encode(access_token+":x"));
Works in FF and Chrome. Doesn't work in IE9 - I am still investigating on that.
HTH
Thomas
我被这个问题所困扰,就像我挠头调试 jQuery 一样,我的具体问题似乎是我正在向一个强制执行 HTTPS 的站点发出 http 请求,所以当浏览器收到指向安全的 301 响应时该网站的版本,看起来 XHR 子系统遵循重定向,但在过程中删除了标头:(
I got bitten by this as well as much as I was scratching my head and debugging jQuery, my specific problem seemed to be I was making an http request to a site that enforced HTTPS, so when the browser got the 301 response pointing to the secure version of the site, it seems like the XHR subsystem follows the redirect but drops the headers in the process :(