Phonegap和宁静的网络服务

时间:2012-02-21 11:21:39

标签: javascript web-services cordova

我想从返回xml的restful webservice中检索数据。我正在使用phonegap。

我试过这段代码,它在InternetExplorer上给我结果,但不在我的手机间隙应用程序上!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
       <title>PhoneGap</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
   <script type="text/javascript" charset="utf-8" src="js/Config/phonegap-0.9.3.js"></script>


   <link rel="stylesheet" href="css/jquery/jquery.mobile-1.0a1.min.css" />
   <link rel="stylesheet" href="css/Style.css" />
   <script src="js/Config/jquery-1.4.3.min.js"></script>
   <script src="js/Config/jquery.mobile-1.0a1.min.js"></script>

<script type="text/javascript">

function getDescription() {
var url = 'http://localhost/prestashop/api/customers/2';
req = new XMLHttpRequest();

req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);
}
function processRequest() {
if (req.readyState == 4) {

if (req.status == 200) {
alert ( "Not able to retrieve description+"+req.responseText );
parseMessages();
} else   {
alert ( "Not able to retrieve description+"+req.responseText+"vide" );
}
}
}
function parseMessages() {
response  = req.responseXML.documentElement;
itemDescription = response.getElementsByTagName('lastname')[0].firstChild.data;
alert ( itemDescription );
}
</script>


</head>
<body>
<button onClick="getDescription()">Ajax call</button>
</body>
</html>

它返回req.status = 0 !!

3 个答案:

答案 0 :(得分:0)

尝试更改此行:

if (req.status == 200) {

进入:

if (req.status == 200 || req.status == 0) {

在从file://协议运行的移动设备上,当成功请求时,您的状态通常为0。

答案 1 :(得分:0)

您在Chrome中获得了哪些结果? Chrome和Android浏览器(以及Safari和BlackBerry Browser)都基于WebKit。

您是否已将访问来源添加到phonegap.xml文件中?

您是否有任何特殊原因没有为您的AJAX使用jQueryMobile?您是否能够使用jQM获得成功的响应?

答案 2 :(得分:0)

尝试

var url = 'http://localhost/prestashop/api/customers/2?PHP_AUTH_USER="password"&ws_key="login"';
相关问题