JQuery 响应类型是字符串而不是文档对象

JQuery Response Type is string instead of document object

我在 Internet Explorer 11 使用 jquery 1.7 处理 post 请求的响应对象时遇到一些问题。

loader.request = jQuery
            .post("/gateway-portal-war/prediction", {
                action : "index",
                matchcode : this.customerMatchCode,
                timespan : timespan
            })
            .done(
                    function(data) {
                        loader.displayData(data
                                .getElementsByTagName("response")[0]);
                        document.getElementById('loader_'
                                + loader.customerMatchCode).style.display = "none";
                    });

在 firefox/chrome 中,对象被视为 html 文档元素,可以很好地进行进一步处理,从文档获取响应等等。

这里的问题是 IE 中的响应是字符串中的 xml,如下所示:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<response>\n ...</response>\n"

我试图通过将响应转换为 activeXObject 来解决这个问题

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;

//xmlDoc.load(data);
xmlDoc.loadXML(data);

但是在调试之后我发现ActiveXObject中几乎所有的东西都没有初始化或者是空的,之后会产生一些错误

您需要在 ajax 调用中定义数据类型;

    .post("/gateway-portal-war/prediction", {
            action : "index",
            dataType: "xml", //here
            matchcode : this.customerMatchCode,
            timespan : timespan
        })

并且jQuery会自动解析你。不定义可能IE11无法解析