大家好,提前谢谢大家。 我正在使用 Phonegap 2.1.0。 在白名单中,一切都被接受..
**<access uri="*" subdomains="true" />
<access origin=".*" subdomains="true"/>**
我正在使用此函数从我的大学 Web 服务器调用远程 php 文件:
var postData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'http://--/smartphone/login.php',
dataType : 'json',
data: postData,
success: function(data){
alert("good");
},
error: function(){
alert('problem!');
}
});
PHP 文件只是为了像这样的调试原因:
<?php
header('Content-Type: application/json');
$sample = array(
'name'=>'My Name',
'email'=>'my_email@example.com'
);
echo json_encode($sample);
?>
但是 ajax 请求没有发生。在 eclipse 中,当我点击提交时,我一直收到这个错误:
JSCallback Error: Request failed with status 0 at file:///android_asset/www/js/cordova-2.1.0.js:3743
此外,我忘了补充一点,我可以从模拟器中将 url 作为链接打开。它工作正常。
*html代码:*
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<script type="text/javascript" charset="utf-8" src="js/cordova-2.1.0.js"></script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title>Smart Greenhouse</title>
</head>
<body>
<div id="container">
<img src="css/images/smart_green_house.png" class="logo" alt="l1ogo" />
<form id="login_form" class="first_display" >
<label class="title"> E-mail:</label>
<input id="1" type="email" class="input" size="45" name="email" />
<label class="title"> password:</label>
<input id="2" type="password" class="input" size="45" name="password" />
<input id="3" class="submit_type" type="submit" value="login" />
</form>
</div>
</body>
</html>
我已经搜索过我想几乎整个网络...搜索了 2 天...一切都已经过测试.. 非常感谢..
最佳答案
最后,经过 4 到 5 天的搜索……我终于找到了答案……我只是在 ajax 回调之后添加了 return false
。我将代码更改如下:
$('form').submit(function(e){
if (password_ok==1 && email_ok==1)
{
var postData = $(this).serialize();
$.ajax({
type: 'POST',
url: 'http://mysite.xx.x/smartphone/login.php',
dataType : 'json',
data: postData,
success: function(data){
alert(data);
},
error: function(){
alert('error!');
}
});
}
else
{
e.preventDefault();
if (email_ok==2)
{
$("#1").css("border-color","red");
$("#1").css("background-color","#FFD8CE");
}
if (password_ok==2)
{
$("#2").css("border-color","red");
$("#2").css("background-color","#FFD8CE");
}
}
return false;
});
现在唯一的问题是它不会在成功时重置页面..而是一步一步...
关于android - Ajax 发布不工作 - Jquery Phonegap Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13289410/