JQuery IE ajax parseerror in local XML file

In IE, jQuery gives me a parseError when I try to read a local XML file. Hope someone can spot it. The 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('.slide').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>
+3
source share
1 answer

Not an ideal solution, but it works:

I quickly discovered that I was not the only one who had this problem:

google search , jQuery error , stacking question

and everything that I seem to be reading indicates how IE reads and parses XML. Found smart solution reading comments here:

blog see comment # 28

. ajax alittle , dataType, № 28 , .

:

//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);
    }
});

methods.parseXML

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 "";
}
+2

Source: https://habr.com/ru/post/1780105/


All Articles