简体   繁体   中英

Phonegap and restful web service

I want to retrieve data from restful webservice that returns xml. I'm using phonegap.

I have tried this code, it gives me result on InternetExplorer but not on my phone gap app!

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

it returns req.status = 0!!

Try changing this line:

if (req.status == 200) {

into:

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

on mobile devices running from the file:// protocol you often get a status of 0 when there is a successful request.

What result do you get in Chrome? Chrome and the Android Browser (along with Safari and the BlackBerry Browser) are all based on WebKit.

Have you added access origin to your phonegap.xml file?

Is there any particular reason you're not using jQueryMobile for your AJAX? Are you able to get a successful response using jQM?

试试看

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

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