我正在使用带有 jQuery 移动的 Phonegap 开发一个 Andriod 应用程序。该应用程序将向服务器提交一些数据,并应等待服务器响应以进一步进行。一切正常,但页面在收到响应之前立即返回。
这是示例服务器 PHP 程序。我放了一个 sleep 命令来确保页面运行 100 秒。
$fp = fopen('data.txt', 'w');
for ($i=0; $i<100; $i++){
sleep(1);
fwrite($fp, 'testdata');
}
fclose($fp);
echo 'success';
AJAX页面提交脚本
$(document).bind('deviceready', function(){
$(function(){
$('form').submit(function(){
var postData = $(this).serialize();
$("#btn_login_submit",this).attr("disabled","disabled");
$.ajax({
type : 'POST',
url : 'http://website.org/phonegap/save.php',
data : postData,
beforeSend : function() {$.mobile.loading('show')},
complete : function() {$.mobile.loading('hide')},
success : function(response){
alert('Success');
},
error : function(){
alert('Sorry, unable to connect to server');
}
});
$("#btn_login_submit").removeAttr("disabled");
});
});
});
问题: 一旦我点击提交按钮,页面就会被提交并立即返回。100 秒后,我收到“成功”的警报消息。我希望页面应该显示加载图像,直到 PHP 程序完成并返回结果。
任何帮助,将不胜感激。