简体   繁体   中英

Jquery xml parse error

I am using ajax to download an xml about Sharepoint 2010 search (packet query). However the issue is when I try to traverse the xml using jquery.

$(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. This works in firefox and chrome, but not IE9. I need it to work in IE9.

It fails on the .text() method, and also if I try .html() it fails there too.

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/ .

Does anyone know how to fix this?

Thanks.

As you said in your question, you need to parse it as XML, not HTML.
Call $.parseXML

Given you're using jQuery already, why don't you just use jQuery.ajax() to make the request? 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. You could also specify it with the dataType option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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