开发者

IE JQuery ajax parseerror on local xml file

开发者 https://www.devze.com 2023-01-30 04:14 出处:网络
In IE, JQuery is giving me a parseError when I\'m trying to read a local XML file. Hoping someone might be able to spot it. Code works fine in FF

In IE, JQuery is giving me a parseError when I'm trying to read a local XML file. Hoping someone might be able to spot it. Code works fine in FF

Jquery in question

$.ajax({
    type: "GET",
    url: settings.PresentationLocation,
    dataType: "xml",
    async: false,
    contentType : 'application/xml',
    success: function(xml){
        //Setup the slides
        $(xml).find('slide').each(function(){
            //Create the slide
            obj.append('<div class="slide"><div class="slideTitle">'+ $(this).find('title').text() +'</div><div class="slideContent">'+ $(this).find('content').text() +'</div></div>');
        });

        totalSlides = obj.children('.sli开发者_运维技巧de').size();

        //Hide all the slides
        obj.children('.slide').hide();
    },

    error: function(xmlReq, status, errorMsg){
        console.log("Error Message: "+ errorMsg);
        console.log("Status: "+ status);
        console.log(xmlReq.responseText);

        throw(errorMsg);
    }
});

XML File

<?xml version="1.0" encoding="UTF-8"?>
<slides>
    <slide>
        <title>Slide 3</title>
        <content>Hi there</content>
    </slide>
</slides>


Not an Ideal Solution but it works:

I quickly found out that I am not the only one having this problem:

google search , JQuery Bug , Stackoverflow question

and everything I seem to read points to how IE reads and parses XML. Found a clever solution reading the comments here:

blog see comment #28

This still didn't work. After some playing with the ajax function alittle I found that if I removed the dataType, in addition to the comment #28 in the blog post, everything worked across browsers.

Final code looks like:

//Retrieve our document
$.ajax({
    type: "GET",
    async: false,
    url: settings.PresentationLocation,
    success:function(results){
        var xml = methods.parseXML(results);

        $(xml).find('slide').each(function(){
            //Create the slide
            obj.append('<div class="slide"><div class="slideTitle">'+ $(this).find('title').text() +'</div><div class="slideContent">'+ $(this).find('content').text() +'</div></div>');
        });

        totalSlides = obj.children('.slide').size();

        //Hide all the slides
        obj.children('.slide').hide();
    },
    error: function(xmlReq, status, errorMsg){
        var errMsg = settings.PresentationLocation + " : "+ errorMsg ;
        throw(errMsg);
    }
});

where methods.parseXML is defined as

parseXML : function(xmlFile){
    if (window.ActiveXObject) {
        //IE
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(xmlFile);
        return doc;
    } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(xmlFile, 'text/xml');
    }

    return "";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消