我想设置接受HTTP标头为text / xml的这个jQuery的code:
I'm trying to set the Accept HTTP header to "text/xml" with this jquery code:
$.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()); } });在Firefox中它的伟大工程。
In firefox it works great.
不过,在IE中,我试图为Accept头设置的值似乎得到追加到末尾,使其成为:接受:* / *,为text / xml 。这将导致我ajax调用,而不是我想要的XML版本返回的HTML版本。
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 8?
Does anybody know how to properly set/clear the Accept header in IE 8?
更新时间:出于某种原因星号并没有出现,因为我进入了他们。 Accept标头在IE中显示为:接受:* / *,为text / xml
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浏览器,而且在Chrome和Safari使用jQuery 1.6.2。此解决方案出现,以达到预期的我已经尝试了所有的浏览器(Chrome浏览器,Safari浏览器,IE浏览器,火狐)。
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).
$.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.
更多推荐
不能正确设置与jQuery的接受HTTP标头
发布评论