我正在尝试使用 ajax 登录。脚本运行良好,我正在登录,但我没有被重定向到dashboard.php
文件。代码如下。请试着帮助我。
阿贾克斯调用
<script type="text/javascript">
$(document).ready(function() {
$("#submit").click(function() {
var dataString = {
username: $("#username").val(),
password: $("#password").val(),
};
$.ajax({
type: "POST",
url: "login-process.php",
data: dataString,
cache: true,
beforeSend: function(){
$('#loading-image').show();
},
complete: function(){
$('#loading-image').hide();
},
success: function(html){
$('.message').html(html).fadeIn(500);
}
});
return false;
});
});
</script>
登录过程.php
<?php
include'config/db.php';
$msg = null;
$date = date('Y-m-d H:i:s');
$uname = (!empty($_POST['username']))?$_POST['username']:null;
$pass = (!empty($_POST['password']))?$_POST['password']:null;
if($_POST){
$stmt = "SELECT * FROM members WHERE mem_uname = :uname";
$stmt = $pdo->prepare($stmt);
$stmt->bindValue(':uname', $uname);
$stmt->execute();
$checklgn = $stmt->rowCount();
$fetch = $stmt->fetch();
if($checklgn > 0){
if(password_verify($pass, $fetch['mem_pass'])){
session_start();
$_SESSION['sanlogin'] = $fetch['mem_id'];
$msg = "<div class='message-success'>Access Granted! Please wait...</div>";
$go_login = header("refresh:2; url=dashboard.php");
}else{
$msg = "<div class='message-error'>Password mismatch. Please try again!</div>";
}
}else{
$msg = "<div class='message-error'>User not found. Please try again!</div>";
}
}
echo $msg;
echo $go_login;
?>
如果使用 AJAX 登录,则必须使用 javascript 进行重定向。重定向 PHP 只会导致 AJAX 调用被重定向,这不会产生所需的结果。
在 ajax 请求的success
子句中,您可以添加window.location.replace("dashboard.php");
In you ajaxsuccess:
使用window.location.href="/uri/to/redirect";
setTimeout(function(){
window.location.href="/uri/to/redirect";
},2000);
在这种情况下header
不能使用 PHP 函数。 你应该像这样重定向你的 $.ajax 函数:
1)你应该在你的login-process.php
中返回JSON:
...
$result = array();
if($_POST){
$stmt = "SELECT * FROM members WHERE mem_uname = :uname";
$stmt = $pdo->prepare($stmt);
$stmt->bindValue(':uname', $uname);
$stmt->execute();
$checklgn = $stmt->rowCount();
$fetch = $stmt->fetch();
if($checklgn > 0){
if(password_verify($pass, $fetch['mem_pass'])){
session_start();
$_SESSION['sanlogin'] = $fetch['mem_id'];
$result = array(
'msg' => "<div class='message-success'>Access Granted! Please wait...</div>",
'redirect' => 'dashboard.php',
);
}else{
$result = array(
'msg' => "<div class='message-error'>Password mismatch. Please try again!</div>",
);
}
}else{
$result = array(
'msg' => "<div class='message-error'>User not found. Please try again!</div>",
);
}
}
echo json_encode($result);
exit;
2) 您应该将参数dataType
添加到服务器答案的$.ajax
调用和进程属性redirect
中(如果此属性存在):
$(document).ready(function () {
$("#submit").click(function () {
var dataString = {
username: $("#username").val(),
password: $("#password").val(),
};
$.ajax({
dataType: 'json', // <-- set expected dataType here
type: "POST",
url: "login-process.php",
data: dataString,
cache: true,
beforeSend: function () {
$('#loading-image').show();
},
complete: function () {
$('#loading-image').hide();
},
success: function (data) {
$('.message').html(data.message).fadeIn(500);
// if we have attribute "redirect" in answer
if(typeof data.redirect !== 'undefined'){
// redirect here after 2 seconds
setTimeout(function(){
document.location.href = data.redirect;
},2000);
}
}
});
return false;
});
});