我是新手机平台和宁静的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. JAVA Restful webservice vs PHP Restful webservice。最好的?
- 2. RestFul PhoneGap和RestFul webservices
- 3. 与localhost webservice连接PhoneGap
- 4. Phonegap + Webservice
- 5. 运行RestEasy Restful Webservice与Tomcat的问题
- 6. 完全RESTFUL WebService与GSON和Java
- 7. Android中的Restful webservice
- 8. Restful webservice返回xml
- 9. PhoneGap中的Webservice
- 10. PhoneGap - 调用asp.net webservice
- 11. RESTful webservice with auth for mobile application
- 12. Restful Webservice,Tomcat错误500
- 13. 回复JSON的Spring restful webservice
- 14. iPhone上托管的RESTful webservice
- 15. Spring Restful Webservice上传CSV
- 16. RESTful WebService不接受@POST?
- 17. 从restful webservice返回json?
- 18. 使用httpbuilder for grails restful webservice
- 19. 从WCF读取cookie RESTful webservice
- 20. Angular不能使用Restful webservice
- 21. JSON输入到ColdFusion webservice + RestFul
- 22. RESTful WebService架构验证
- 23. 管理会话phonegap restful API?
- 24. Phonegap jQuery Ajax来调用webservice
- 25. 在PhoneGap中使用ASMX webservice
- 26. 简单的webservice ane之间的区别Restful webservice
- 27. 如何在Android中集成RESTful webservice
- 28. RESTFul Webservice注释中的问题
- 29. 在Restful webservice中删除操作
- 30. 您是否需要RESTful webservice的接口?
谢谢。我已经做了这样的事情,但问题是,当我在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