我是新手机平台和宁静的web服务。我的问题是我怎么能融合他们两个,所以我有一个应用程序,将与宁静的web服务同步。我已经尝试了jsonp的示例安静服务,但phonegap不会加载服务或者我可能错过了某些东西。谢谢。Phonegap与Restful webservice
1
A
回答
3
下面的代码可以帮助您了解如何调用Web服务,并和解析它来显示结果
<html>
<head>
<script src="js/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script>
function bodyload(){
alert("We are calling jquery's ajax function and on success callback xml parsing are done");
$.ajax({
url:'http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml',
dataType:'application/xml',
timeout:10000,
type:'POST',
success:function(data) {
$("#bookInFo").html("");
$("#bookInFo").append("<hr>");
$(data).find("Book").each(function() {
$("#bookInFo").append("<br> Name: " + $(this).find("name").text());
$("#bookInFo").append("<br> Address: " + $(this).find("address").text());
$("#bookInFo").append("<br> Country: " + $(this).find("country").text());
$("#bookInFo").append("<br><hr>");
});
},
error:function(XMLHttpRequest,textStatus, errorThrown) {
alert("Error status :"+textStatus);
alert("Error type :"+errorThrown);
alert("Error message :"+XMLHttpRequest.responseXML);
$("#bookInFo").append(XMLHttpRequest.responseXML);
}
});
}
</script>
</head>
<body onload="bodyload()">
<button onclick="bodyload()">Ajax call</button>
<p id="bookInFo"></p>
</body>
</html>
1
它的工作方式相同(使用JSON)正常的Web项目:
$.ajax({
url: 'http://...',
type: 'POST',
dataType: 'json',
data: data,
success: : function(data) {
//...
},
error: function(xhr, textStatus, errorThrown) {
//...
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Accept', 'application/json');
}
});
唯一的区别是在PhoneGap中,您不必担心相同的原产地政策问题。这使得JSONP的使用不是非常必要的。除非你正在使用只处理JSONP而不处理JSON的服务器。
1
我改变你的ajax调用稍微像这样。我们需要发布请求。另外jquery.js应该是第一个。尝试调用设备就绪功能。
$.ajax({
url:'http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml',
dataType:'xml',
type:'get',
cache: false,
相关问题
- 1. PhoneGap中的Webservice
- 2. Restful webservice返回xml
- 3. PhoneGap - 调用asp.net webservice
- 4. RESTful WebService不接受@POST?
- 5. 使用httpbuilder for grails restful webservice
- 6. 从restful webservice返回json?
- 7. 从WCF读取cookie RESTful webservice
- 8. Restful Webservice,Tomcat错误500
- 9. Spring Restful Webservice上传CSV
- 10. RESTful WebService架构验证
- 11. Angular不能使用Restful webservice
- 12. JSON输入到ColdFusion webservice + RestFul
- 13. iPhone上托管的RESTful webservice
- 14. RESTful webservice with auth for mobile application
- 15. Phonegap jQuery Ajax来调用webservice
- 16. 简单的webservice ane之间的区别Restful webservice
- 17. 如何获取RESTful webservice的请求URI
- 18. Baisc Restful Webservice项目404没有找到
- 19. 如何从Restful webservice返回Java.util.ArrayList?
- 20. ReSTful webservice真的是我的答案吗?
- 21. 如何在Android中集成RESTful webservice
- 22. 您是否需要RESTful webservice的接口?
- 23. Restful webservice查询参数问题
- 24. 用java客户端消费WCF restful webservice?
- 25. Restful WebService部署策略 - 最佳实践
- 26. Jquery ajax在Phonegap内调用RESTful API
- 27. 与瓶的RESTful
- 28. 与.NET的Webservice
- 29. PhoneGap与Titanium
- 30. 的PhoneGap与iPhone
谢谢。我已经做了这样的事情,但问题是,当我在android模拟器上运行phonegap它不会获取任何数据(我已包含警报以显示它的工作,但警报事件不会触发。) – jongbanaag 2012-02-20 10:15:39
您可能会缺少这个http://stackoverflow.com/a/9359170/28557 – 2012-02-20 10:28:56
我已经想通了,我惊讶它仍然无法正常工作。它在本地工作有问题吗?因为我仍然在我的模拟器上运行它,其余的服务在本地运行。 – jongbanaag 2012-02-21 01:14:12