简体   繁体   English

jQuery XML解析错误

[英]Jquery xml parse error

I am using ajax to download an xml about Sharepoint 2010 search (packet query). 我正在使用ajax下载有关Sharepoint 2010搜索(数据包查询)的xml。 However the issue is when I try to traverse the xml using jquery. 但是问题是当我尝试使用jquery遍历xml时。

$(document).ready(function() {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };

    var timeout = null;
    $('#SearchInputBox').on('keyup', function () {
        var text = $(this).val();
        if (timeout !== null) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(function () {
            $('#LoadImage').show();
            DoSearch(text);
        }, 1000);
    });
});

function searchComplete(xData, status) {
    $('#LoadImage').hide();
    var search_results = $("#result");
    search_results.html('');

    $(xData.responseXML).find("QueryResult").each(function() {
        var err;
        try {
            var obj = $(this).eq(0);

            var text = obj.text(); // works in Firefox and Chrome but not IE9
            //var text = obj.text;
            //var text = obj.xml;

            var x = $("<xml>" + text + "</xml>");
            var docs = x.find("LinkUrl");
            var len = docs.length;
            var link;

            for(var i=0; i<len; i+=1) {
                link = $(docs[i]).text();
                if (link.toLowerCase().endsWith(".pdf")) {
                    search_results.append(link + "<br/>");
                }
            }
        } catch(err) {
            search_results.text("An error occured: " + err);
        }
    });
}

In this code, I retrieve the xml data, then try to display it on the browser. 在这段代码中,我检索了xml数据,然后尝试将其显示在浏览器中。 This works in firefox and chrome, but not IE9. 这适用于Firefox和Chrome,但不适用于IE9。 I need it to work in IE9. 我需要它才能在IE9中工作。

It fails on the .text() method, and also if I try .html() it fails there too. 它在.text()方法上失败,并且如果我尝试.html()它也会在那里失败。

I think there might be some browser specific things going on with the .text() method or maybe I need to parse the xml using http://api.jquery.com/jQuery.parseXML/ . 我认为.text()方法可能会发生一些特定于浏览器的事情,或者我需要使用http://api.jquery.com/jQuery.parseXML/来解析xml。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

Thanks. 谢谢。

As you said in your question, you need to parse it as XML, not HTML. 正如您在问题中所说的那样,您需要将其解析为XML,而不是HTML。
Call $.parseXML 调用$.parseXML

Given you're using jQuery already, why don't you just use jQuery.ajax() to make the request? 考虑到您已经在使用jQuery,为什么不只使用jQuery.ajax()发出请求? It will help with a lot of cross-browser inconsistencies, and jQuery will make an "intelligent guess" at the response type and parse it automatically for you. 这将有助于解决许多跨浏览器的不一致问题,并且jQuery将对响应类型进行“智能猜测”,并自动为您解析。 You could also specify it with the dataType option. 您也可以使用dataType选项指定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM