2013-03-26 31 views
0

我知道我的问题可能会问以前几次。我尝试了很多解决方案,但它对我没有用。使用JQuery和PhoneGap解析XML

我想构建一个使用JQuery和PhoneGap从XML文件中获取数据的移动应用程序。

这是我的XML文件:

<book> 
<chapter id="1"> 
<title>title 1</title> 
<question id="1.1"> Question 1 </question> 
<answer id="1.1.1"> answer for q1 </answer> 
</chapter> 

<chapter id="2"> 
<title>title 2</title> 
<question id="2.1"> Question 2 </question> 
<answer id="1.2.1"> answer for q2 </answer> 
</chapter> 
</book> 

,这是我的JavaScript:

$(document).bind("pageshow", function() { 

$.ajax({ 
     type: 'GET', 
     url:'Book.xml', 
     datatype:'xml', 
     success:function(xml){ 
      alert("success"); 
     } 
    }); 
}); 

如果我从来没有表现出成功的警报功能,但是如果在此代码显示的警报 的$(document).bind( “pageshow”,函数(){

$.ajax({ 
     type: 'GET', 
     url:'Book.xml', 
     datatype:'xml', 
     success: alert("success"); 

    }); 
}); 

我坚持在这里,我CA不添加任何进一步的代码。

+0

这就是我在控制台中得到的结果 XMLHttpRequest无法加载file:///Book.xml。 Access-Control-Allow-Origin不允许Origin null。 – user1748687 2013-03-26 15:02:38

回答

0

第二个版本简单地调用alert并将返回值(undefined)分配给成功成员。这就是为什么你看到提醒消息框。

由于许多原因,第一个版本可能会失败。首先,您应确认您确实正在接收数据(例如,通过使用浏览器中的开发人员工具)