【发布时间】:2013-01-12 13:18:03
【问题描述】:
可能重复:
How to manage a redirect request after a jQuery Ajax call
我有一个使用 Jquery 在客户端验证的表单。然后,Jquery 将此数据传递给 php 脚本以进行服务器端验证。在 php 脚本结束时,我尝试在成功时重定向,但没有发生重定向。
Jquery 会干扰重定向吗?如何让 php 脚本重定向。不想用 jquery 重定向,因为我将使用会话并希望在重定向时保留会话数据。
代码如下:
查询:
$.ajax({
//this is the php file that processes the data and send mail
url: "includes/process/processAdminLogin.php",
//POST method is used
type: "POST",
//pass the data
data: dataString,
//indicate result is text
dataType: "text",
//Do not cache the page
cache: false,
//success
success: function (data) {
$('input').removeAttr('disabled'); //enable input fields again.
if(data == "fail"){
$("#statusMessage").html("");
$("#statusMessage").html("<p class='statusBoxWarning'>Username and password do not match!</p>");
$('button').removeAttr('disabled');
document.forms[0].reset();
}
}
});
PHP
if($correctPass == 1){
ob_start();
session_start();
$_SESSION['usernameIdentity'] = $userName;
unset($userName, $userPass);
header("Location: ../../adminDashboard.html");
exit;
}else{
echo "fail";
}
php 脚本到达重定向部分并挂断。我真的很想保留 jquery 功能以及 php 重定向。有没有更好的方法?
谢谢!
FINAL WORKING SOLUTION:
好的,在这篇文章和其他类似文章的输入之后,这就是我所拥有的工作解决方案。它可能不是最有效或最漂亮的解决方案,但它确实有效,而且现在必须这样做。
JQUERY
$.ajax({
//this is the php file that processes the data and send mail
url: "includes/process/processAdminLogin.php",
//GET method is used
type: "POST",
//pass the data
data: dataString,
//indicate result is text
dataType: "text",
//Do not cache the page
cache: false,
//success
success: function (data) {
$('input').removeAttr('disabled'); //enable input fields again.
if(data == "success"){
$('#statusMessage').html('<form action="http://www.example.com/test/userRegistration/adminDashboard.html" name="userSubscription" method="post" style="display:none;"><input type="text" name="username" value="' + reg_contact_username + '" /><input type="text" name="password" value="' + reg_contact_password + '" /></form>');
document.forms['userSubscription'].submit();
}else{alert("couldn t redirect");}
}
});
PHP
if($correctPass == 1){
echo "success";
}else{
echo "fail";
}
接收重定向页面将不得不再次验证用户名和密码,因此验证将完成 2 次......这确实是低效的。如果有人可以为我提供更好的解决方案 - 请!写出样本也会有所帮助。
谢谢!
【问题讨论】: