不能正确设置与jQuery的接受HTTP标头 [英] Cannot properly set the Accept HTTP header with jQuery

查看:129
本文介绍了不能正确设置与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标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆