PhoneGap和PrestaShop网络服务

PhoneGap and PrestaShop web service

本文关键字:网络服务 PrestaShop PhoneGap      更新时间:2023-09-26

我想从PrestaShop网站的web服务(返回XML)中检索数据。我使用PhoneGap Android。我试过这个代码,它在InternetExplorer上给了我一个很好的结果,但在我的PhoneGap应用程序上没有。

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

我也尝试过这个代码,但我面临同样的问题。

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

这可能是因为您没有授权访问外部主机。

你在/res/xml/PhoneGap.xml 中添加了这个吗

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

在执行任何代码之前,您可能还需要等待PhoneGap准备就绪

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
    // YOU CODE
}

您在文件协议中遇到了一个熟悉的XHR问题。线路:

if (req.status == 200) {

应重写为:

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

当您从文件//执行XHR时,状态通常报告为0。将其视为200状态是完全可以的。

我在请求中使用了以下代码段,它运行良好,希望能有所帮助。您可以引用链接:XUI库,并将所需的属性添加到xhr调用中。

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");
            }
        }
        );