无法使用 jQuery 正确设置 Accept HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1145588/
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
Cannot properly set the Accept HTTP header with jQuery
提问by
I'm trying to set the Accept HTTP header to "text/xml" with this jquery code:
我正在尝试使用以下 jquery 代码将 Accept HTTP 标头设置为“text/xml”:
$.ajax({
beforeSend: function(req) {
req.setRequestHeader("Accept", "text/xml");
},
type: "GET",
url: "[proper url]",
contentType: "text/plain; charset=utf-8",
dataType: ($.browser.msie) ? "text" : "xml",
username: '---',
password: '-------',
success: function(data) {
var xml;
if (typeof data == "string") {
alert("Data is string:" + data);
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
alert("Data is not string:" + $(xml).text());
}
// Returned data available in object "xml"
//alert("Status is: " + xml.statusText);
$("#ingest_history").html($(xml).text());
}
});
In firefox it works great.
在 Firefox 中它工作得很好。
But in IE, the value that I am trying to set for the Accept header seems to get appended onto the end so it becomes: Accept: */*, text/xml
. This causes my ajax call to return the html version as opposed to the xml version which I want.
但是在 IE 中,我尝试为 Accept 标头设置的值似乎附加到末尾,因此它变为:Accept: */*, text/xml
. 这导致我的 ajax 调用返回 html 版本,而不是我想要的 xml 版本。
Does anybody know how to properly set/clear the Accept header in IE 8?
有人知道如何在 IE 8 中正确设置/清除 Accept 标头吗?
Updated: For some reason the asterisks weren't appearing as I entered them. The Accept Header in IE appears to be: Accept: */*, text/xml
.
更新:由于某种原因,星号在我输入时没有出现。IE 中的 Accept Header 似乎是:Accept: */*, text/xml
.
回答by Joel Westberg
I also had trouble with this, not just in IE but also in Chrome and Safari using jQuery 1.6.2. This solution appears to work as intended in all browsers I've tried (Chrome, Safari, IE, Firefox).
我也遇到了这个问题,不仅在 IE 中,而且在使用 jQuery 1.6.2 的 Chrome 和 Safari 中也是如此。此解决方案似乎在我尝试过的所有浏览器(Chrome、Safari、IE、Firefox)中都能正常工作。
$.ajax({
headers: {
Accept : "text/plain; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
},
data: "data",
success : function(response) {
...
}
})
Try that if this is still giving you trouble.
如果这仍然给您带来麻烦,请尝试。
回答by gnarf
Using jQuery 1.5+ you can set the accepts headers per dataType
so you can do something like this:
使用 jQuery 1.5+,您可以设置每个接受标头,dataType
以便您可以执行以下操作:
$.ajax({
dataType: ($.browser.msie) ? "text" : "xml",
accepts: {
xml: "text/xml",
text: "text/xml"
}
});
回答by Jim Ferrans
Your problem seems to be the one described here: http://www.grauw.nl/blog/entry/470. The issue is that the XMLHttpRequest specificationcurrently states that user agents should not set any Accept headers by default for the request, so that req.setRequestHeader() can just append new Accepts. Unfortunately browsers don't yet adhere to this. The problem writeup lets you test your browser to see if it works properly, and unfortunately IE7, Chrome, Safari, Firefox, and Opera all fail.
您的问题似乎是这里描述的问题:http: //www.grauw.nl/blog/entry/470。问题是XMLHttpRequest 规范当前声明用户代理默认情况下不应为请求设置任何 Accept 标头,因此 req.setRequestHeader() 只能附加新的 Accepts。不幸的是,浏览器还没有遵守这一点。问题报告让您测试浏览器是否正常工作,不幸的是 IE7、Chrome、Safari、Firefox 和 Opera 都失败了。
Laurens Grauw also talks about the effects of first trying to null out the Accept header with
Laurens Grauw 还谈到了首先尝试用
setRequestHeader('Accept', '')
or
或者
setRequestHeader('Accept', null)
These might help here.
这些可能会有所帮助。
Ugly server-side hacks: If you have control over your server-side app you can hardwire it to always return XML, add support for a custom media type like "application/i-really-want-xml", or add support for a custom HTTP header like "X-Accept".
丑陋的服务器端黑客:如果你可以控制你的服务器端应用程序,你可以将它硬连线以始终返回 XML,添加对自定义媒体类型的支持,如“application/i-really-want-xml”,或添加对自定义 HTTP 标头,如“X-Accept”。
回答by EricLaw
I think the original poster might have been referring to this link: http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspxhowever, this doesn't explain the behavior you see.
我认为原始海报可能是指这个链接:http: //blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx但是,这并没有解释你看到的行为。
IE does not, by itself, have the behavior you describe, and setting the Accept header via XMLHTTPRequest should work properly. I've tested in IE8 to confirm.
IE 本身不具有您描述的行为,并且通过 XMLHTTPRequest 设置 Accept 标头应该可以正常工作。我已经在 IE8 中进行了测试以确认。
It's possible there's an issue in your version of jQuery, or perhaps you have some plugin mangling your traffic?
您的 jQuery 版本可能存在问题,或者您的某些插件可能会影响您的流量?
回答by Adam
Although this isnt how the documentation states it needs to be done, it is what worked for me.
虽然这不是文档说明它需要完成的方式,但它对我有用。
jQuery.ajax({
type: "POST",
url: "...",
data: ...,
contentType: "text/xml",
beforeSend: function(req) {
req.setRequestHeader("Accept", "text/xml");
}, ...});
回答by BStruthers
I don't believe that IE(any version) plays nice with the Accept header. See this link: [http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx]
我不相信 IE(任何版本)与 Accept 标头搭配得很好。请参阅此链接:[ http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx]
A possible solution might be to check the User Agent to see if it's IE. If it is, then check for the presence of text/xml.
一个可能的解决方案可能是检查用户代理以查看它是否是 IE。如果是,则检查 text/xml 是否存在。
Good Luck!
祝你好运!
Edit:
编辑:
Opps on the link. My hunch was IE is always adding the /and setting the accept header just adds the desired mime type after the /.
链接上的 Ops。我的预感是 IE 总是添加/并且设置接受标头只是在/之后添加所需的 mime 类型。