不能正确设置与jQuery的接受HTTP标头

编程入门 行业动态 更新时间:2024-10-19 16:37:45
本文介绍了不能正确设置与jQuery的接受HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想设置接受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标头

本文发布于:2023-11-16 16:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   jQuery   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!