omega
omega

Reputation: 43833

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.

Upvotes: 0

Views: 704

Answers (2)

Adam
Adam

Reputation: 997

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.

Upvotes: 0

SLaks
SLaks

Reputation: 887195

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

Upvotes: 1

Related Questions