Phonegap and restful web service

Phonegap and restful web service

本文关键字:service web restful and Phonegap      更新时间:2023-09-26

我想从返回xml的restful Web服务中检索数据。我在用phonegap。

我试过这个代码,它在InternetExplorer上给了我结果,但在我的手机gap应用程序上没有!

    <!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!!

尝试

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

尝试更改此行:

if (req.status == 200) {

进入:

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

在从file://protocol运行的移动设备上,当请求成功时,您通常会得到0的状态。

您在Chrome中会得到什么结果?Chrome和Android浏览器(以及Safari和BlackBerry浏览器)都基于WebKit。

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

有没有什么特别的原因,你不使用jQueryMobile的AJAX?您是否能够使用jQM获得成功的响应?