PhoneGap 和 PrestaShop 网络服务

发布于 2025-01-08 18:06:40 字数 2974 浏览 1 评论 0原文

我想从我的 PrestaShop 网站的 Web 服务(返回 XML)检索数据。我使用PhoneGap Android。 我尝试了这段代码,它在 Internet Explorer 上给了我一个很好的结果,但在我的 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若沐 2025-01-15 18:06:40

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

您是否在 /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
}

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
}
菩提树下叶撕阳。 2025-01-15 18:06:40

您遇到了一个熟悉的 XHR 文件协议问题。该行:

if (req.status == 200) {

应重写为:

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

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

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.

云巢 2025-01-15 18:06:40

我使用以下代码片段来满足我的请求,效果很好,希望对您有所帮助。
您可以参考链接: 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");
            }
        }
        );

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");
            }
        }
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文