1

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

  • check out http://groups.google.com/group/phonegap/browse_thread/thread/2a31e83252f2a140?pli=1 – ghostCoder Feb 21 '12 at 11:37
  • try the things in above link and tell if anything works – ghostCoder Feb 21 '12 at 12:45
  • i didnt find the solution in this link, i have tried another code without XMLHttpRequest, but IE display the result, and nothing is displayed in my phonegap app! this is the link of the new idea: http://stackoverflow.com/questions/8498098/parse-xml-with-jquery-ajax-request –  Feb 21 '12 at 12:56

3 Answers3

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.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • now, it pass to the execution of th function parseMessages(), but it is blocked on the first instruction response = req.responseXML.documentElement; –  Feb 21 '12 at 14:00
  • @simon can you elaborate on the status == 0 issue you're noticed with file:// protocol requests? Im getting the status 0 intermittently from my app, and I's causing troubles. However, it only happens when the server respond with a 401 (which is also intermittent). Are the two related, perhaps? – wprater Nov 01 '12 at 22:48
  • If I remember correctly XHR needs the HTTP status response to be returned correctly but since you are making your request to the local file system there is no web server to fill in that information. Ergo you get a 0 for the status code. – Simon MacDonald Nov 02 '12 at 14:26
0

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?

darryn.ten
  • 6,784
  • 3
  • 47
  • 65
  • Access origin is added like this: but i have added this permission to Androidanifest. xml: but now the code return a 401 status!! –  Feb 24 '12 at 10:00
  • 401 is unauthorized. Is this what you were expecting? – darryn.ten Feb 24 '12 at 10:07
  • no, i want to access to the web service(status=200). This is my actual problem http://stackoverflow.com/questions/9431877/xmlhttprequest-returns-401-on-phonegap-android-application –  Feb 24 '12 at 14:35
0

Try with

var url = 'http://localhost/prestashop/api/customers/2?PHP_AUTH_USER="password"&ws_key="login"';
Aminesrine
  • 2,082
  • 6
  • 35
  • 64