2010-12-13 82 views
3

在IE中,当我试图读取本地XML文件时,JQuery给了我一个parseError。希望有人能够发现它。代码在FF工作正常本地xml文件IE浏览器JQuery ajax parseerror

jQuery的问题

$.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文件

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

语法对我来说很好。你在ie8中使用开发人员工具测试了这个吗?对它给你的错误有更多具体细节?如果您不能在那里放置断点,我会添加警报,以便您可以准确查看错误的位置。 – wajiw 2010-12-13 23:12:30

回答

2

不是一个理想的解决方案,但它的工作原理:

我很快发现我不是唯一有这个问题的人:

google searchJQuery BugStackoverflow question

,一切我似乎读点IE如何读取并解析XML。发现一个聪明的解决方案在这里阅读注释:

blog见注释#28

这仍然没有奏效。在玩了一些ajax函数之后,我发现如果我删除了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 ""; 
}