简体   繁体   中英

PhoneGap and PrestaShop web service

I want to retrieve data from web service that (returns XML) of my PrestaShop web site. I use PhoneGap Android. I tried this code it gives me a good result on Internet Explorer, but not on my PhoneGap application.

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

I have also tried this code, but I face the same problem.

<!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 src="jquery.form.js"></script>


<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "http://localhost/prestashop/api/customers/2",
//dataType: "xml",
success: parseXml
});

function parseXml(xml) {alert("a");
  $(xml).find("customer").each(function()  {alert("b");

        $("#dropdownlist").append("<hr>"+$(this).find("lastname")[0].firstChild.data+"<hr>");
      });
}

});
</script>  
</head>

<body>  
<div id="dropdownlist" />
</body>  
</html>

It could be because you didn't authorize the access to external hosts.

Did you add this in /res/xml/PhoneGap.xml

<?xml version="1.0" encoding="utf-8"?>
<phonegap>
    <access origin="*"/>
    <log level="DEBUG"/>
</phonegap>

You might also need to wait for PhoneGap to be ready before executing any code

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    // YOU CODE
}

You are falling into a familiar problem with XHR from the file protocol. The line:

if (req.status == 200) {

should be re-written as:

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

Often the status is reported as 0 when you do XHR from file://. It is perfectly okay to treat it as a 200 status.

I use the following snippet for my request, and it works fine, hope it helps. You can refer the link : XUI library , and add the required attributes to the xhr call.

x$.data = {};
        x$("#YOUR_HTML_ELEMENT_ID").xhr("YOUR_URL",
        { 
            callback: function(){
            XMLresponse =  this.responseText; 
            alert("RESULT:"+XMLresponse )
        },
            error:function(){
               alert("ANY ERROR HANDLE HERE");
            }
        }
        );

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