PhoneGap 和 PrestaShop 网络服务
我想从我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是因为您没有授权访问外部主机。
您是否在
/res/xml/PhoneGap.xml
中添加了此内容?您可能还需要等待 PhoneGap 准备好才能执行任何代码
It could be because you didn't authorize the access to external hosts.
Did you add this in
/res/xml/PhoneGap.xml
You might also need to wait for PhoneGap to be ready before executing any code
您遇到了一个熟悉的 XHR 文件协议问题。该行:
应重写为:
当您从 file:// 执行 XHR 时,状态通常报告为 0。将其视为 200 状态是完全可以的。
You are falling into a familiar problem with XHR from the file protocol. The line:
should be re-written as:
Often the status is reported as 0 when you do XHR from file://. It is perfectly okay to treat it as a 200 status.
我使用以下代码片段来满足我的请求,效果很好,希望对您有所帮助。
您可以参考链接: XUI 库 ,并将所需的属性添加到 xhr 调用中。
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.