我试图在我的phonegap android应用程序从本地服务器检索json数据。我放了一个提交输入来获取数据。我正在使用方法$。jquery中的Ajax。我的问题是,当我点击提交,什么都没有显示,但页面刷新。我不知道该怎么解决。
你能帮帮我吗?提前谢谢。
(ps:返回的json是正确的,我已经改变了config.xml中的访问)
下面是完整的代码:
<!DOCTYPE html>
<html>
<head>
<title>Application test</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
<link href="css/jquery.mobile-1.0rc1.min.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.6.4.js"></script>
<script src="js/jquery.mobile-1.0rc1.min.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
}
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('#testform').submit(function(){
$('#content').html("<b>Chargement...</b>");
$.ajax({
url: "http://localhost/projects/api/getAllCategorie.php"
}).done(function(data){
$("#content").html('<p> ID: ' + data.categories[0].id + '</p>');
log('erreur');
}).fail(function(){
alert("Erreur!!");
$("#content").html('erreur');
log('erreur');
});
return false;
};
</script>
</head>
<body>
<img src= "css/images/logo-annuaire-mayotte.png">
<ul data-role="listview" data-filter="true" data-filter placeholder="Rechercher..." data-inset="true">
<li><a href="#">Restaurants</a></li>
<li><a href="#">Bâtiments</a></li>
<li><a href="#">Numéros utiles</a></li>
</ul>
<form id='testform'>
<div><input type="submit" id="driver" value="Synchronisation" /></div>
</form>
<div id="content">
</div>
</body>
</html>
-
如果你想连接到你的本地主机php文件使用端口号(为我工作)或你的实际系统的IP地址,像这样
http:// localhost:8080/projects/api/getAllCategorie.php,(for emulator) http:// 10.0.2.2/projects/api/getAllCategorie.php
-
关闭文档准备功能
$(document).ready(function(){ $('#driver').click(function(event){ event.preventDefault(); // To prevent default action of the form $('#content').html("<b>Chargement...</b>"); $.ajax({ url: "http://localhost:8080/projects/api/getAllCategorie.php", dataType:"json", beforeSend:function(){ alert("Request is going to send now");// place your loader }, success:function(data){ $("#content").html('<p> ID: ' + data.categories[0].id + '</p>'); log('erreur'); }, error:function(xhr, status, error){ alert(xhr.responseText); $("#content").html('erreur'); log('erreur'); } }); return false; }); });
HTMl:用这个输入标签替换你的表单标签
<div><input type="button" id="driver" value="Synchronisation" /></div>