我使用ajax函数发送数据。在php进程中,我运行phpmailer发送电子邮件使用smtp gmail。一切都进行得很顺利,但是有些情况下我想让信息传递给用户等待正在进行的过程。我的代码:
LoadingProccess();
var FormData = $('#subscribe').serialize();
$.ajax({
type: "POST",
url: "proses_corporate.php",
data: FormData,
cache: false,
dataType: "json",
success: function(data){
var cek = data.result;
$.unblockUI();
if(cek=='Success'){
$('#subscribe')[0].reset();
$.blockUI({
message: '<h4>'+data.status+'</h4>',
timeout: 8000 ,
css: {
top: ($(window).height() - 100) /2 + 'px',
left: ($(window).width() - 650) /2 + 'px',
width: '650px'
});
setTimeout(function() {
window.location.href = 'http://www.domain.com/';
}, 8000);
}
setTimeout(function() {
window.location.href = 'http://www.domain.com/';
}, 8000);
}
},
error: function() {
alert('Error');
}
});
场景:当我提交时,应用程序将被锁定,并向用户显示流程正在进行的信息。为此,php需要几秒钟来处理通过smtp发送邮件的gmail。
在我的php echo json_encode给给命令的jquery显示信息,进程已经成功。My php code:
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return $error;
} else {
$error = 'Message sent!';
return $error;
}
}
使用smtpmailer('bertho_joris@yahoo.co.id','berthojoris@gmail.com','GMAIL','Testing Form GMail','Test...Test...Test...Test...');
echo json_encode(array('status' => 'Data has been sent.', 'result' => 'Success'));
与上面的代码一样,表单将在成功处理后路由到特定的页面。在我的例子中,jQuery如何等待动态php的结果才能翻页?
你是否必须在jQuery上使用超时:10000 ?它似乎不是动态的,因为时钟是手动设置的。还有别的办法吗?在等待响应时使ajax请求超时时间更长。如
$.ajax({
type: "POST",
url: "proses_corporate.php",
data: FormData,
cache: false,
timeout: 30000, // 3 second
dataType: "json",
success: function(res){ },
failed: function(err){ }
}
你可以在页面上放置一个蒙版,让它在ajaxStart()中可见,然后在ajaxStop()函数中消失或重定向页面
您可以使用beforeend函数来添加遮罩。在"完成"功能下,取下口罩。