简体   繁体   中英

XML reading using jQuery is not working in ie6 and ie8

XML reading using jQuery is not working in ie6 and ie8. i've used the below code.. the alert is not coming in ie8, ie6, i've not tested with other internet explorer versions.

$(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "xml/contextMenu.xml",
        dataType: "xml",
        success: function(xml) {
            alert('hi');            
        }
    });
});

but it is working in mozilla firefox 3.6.3. Anybody has any idea what may be the problem... Please help me...

This worked for me

function text2XML(txt)
{
    var xmlDoc;
    if (window.DOMParser)
    {
        xmlDoc=(new DOMParser()).parseFromString(txt,"text/xml");
    }
    else
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(txt);
    } 
    return xmlDoc;    
}

IE 6-8 doesn't read the mime type properly, so you pass it as a text file and than you use the function .parseXML to convert it to a xml file

  $(document).ready(function() {  
 $.ajax({
    type: "GET",
    url: "menu.xml",
    dataType:"text",
    success: selectXml
});
   function selectXml (xml) {
   alert('hello');
  }

try this before you start the $.ajax() statement

function parseXML(xml){
if (jQuery.browser.msie) {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(xml);
xml = xmlDoc;
}
return xml;
}

then, in success, add: var newXML = parseXML(xml);

change your references of xml to newXML and you should be good.

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