2012-02-20 65 views
1

我是新手机平台和宁静的web服务。我的问题是我怎么能融合他们两个,所以我有一个应用程序,将与宁静的web服务同步。我已经尝试了jsonp的示例安静服务,但phonegap不会加载服务或者我可能错过了某些东西。谢谢。Phonegap与Restful webservice

回答

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> 
+0

谢谢。我已经做了这样的事情,但问题是,当我在android模拟器上运行phonegap它不会获取任何数据(我已包含警报以显示它的工作,但警报事件不会触发。) – jongbanaag 2012-02-20 10:15:39

+0

您可能会缺少这个http://stackoverflow.com/a/9359170/28557 – 2012-02-20 10:28:56

+0

我已经想通了,我惊讶它仍然无法正常工作。它在本地工作有问题吗?因为我仍然在我的模拟器上运行它,其余的服务在本地运行。 – jongbanaag 2012-02-21 01:14:12

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,