无法使用 jQuery 正确设置 Accept HTTP 标头 [英] Cannot properly set the Accept HTTP header with jQuery

查看:30
本文介绍了无法使用 jQuery 正确设置 Accept HTTP 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此 jquery 代码将 Accept HTTP 标头设置为text/xml":

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 标头设置的值似乎附加到末尾,因此它变为:Accept: */*, text/xml.这会导致我的 ajax 调用返回 html 版本,而不是我想要的 xml 版本.

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 中正确设置/清除 Accept 标头吗?

Does anybody know how to properly set/clear the Accept header in IE 8?

更新:由于某种原因,星号在我输入时没有出现.IE 中的 Accept Header 显示为:Accept: */*, 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 中,而且在使用 jQuery 1.6.2 的 Chrome 和 Safari 中也是如此.此解决方案似乎在我尝试过的所有浏览器(Chrome、Safari、IE、Firefox)中都能正常工作.

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

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