Php重定向导致302 for ajax重定向
这是我的登录控制器的核心脚本,这意味着所有用户的清理和验证都完成了 LoginController.php中的代码Php重定向导致302 for ajax重定向,php,jquery,ajax,Php,Jquery,Ajax,这是我的登录控制器的核心脚本,这意味着所有用户的清理和验证都完成了 LoginController.php中的代码 if ( password_verify($valid['pass'], $pass) ) { // My session stuffs header('Content-Type: application/json'); echo json_encode (array( 'result' => 'success', '
if ( password_verify($valid['pass'], $pass) ) {
// My session stuffs
header('Content-Type: application/json');
echo json_encode (array(
'result' => 'success',
'msg' => 'Login successful..! You will be redirected in a moment'
));
redirect('user/profile'); //Ajax redirect only works when I remove this.
exit;
}
login.php中的代码
try {
LoginController::UserLogin(); // Calling my method inside my controller
} catch (Exception $e) {
echo json_encode(array(
'error' => array(
'msg' => $e->getMessage()
),
), JSON_PRETTY_PRINT);
header('Content-Type: application/json');
exit;
}
<form id="login_form" action="<?php echo htmlspecialchars(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)); ?>" class="form" method="POST">
<div class="form-group">
<input type="text" name="login_username" placeholder="Enter username" class="form-control" />
</div>
<div class="form-group">
<input type="password" name="login_pass" placeholder="Enter password" class="form-control" />
</div>
<div class="form-group">
<input type="text" name="login_security" placeholder="Enter Security 25 * 2" class="form-control" />
</div>
<button class="btn btn-green" id="login-btn">Login</button>
</form>
试试看{
LoginController::UserLogin();//在我的控制器内调用我的方法
}捕获(例外$e){
echo json_编码(数组(
'错误'=>数组(
'msg'=>$e->getMessage()
),
),JSON_PRETTY_PRINT);
标题('Content-Type:application/json');
出口
}
试试这个,看看它是否有效:
在LoginController.php中:删除echo
部分后的重定向
在Login.php中,将标题置于echo
部分之前
在Ajax部分中,更改成功部分,如下所示:
success: function(data) {
console.log(data);
if (data.error) {
$('.error').html('<div class="alert alert-danger">' + data.error.msg + '</div>');
}
if ( data.result === 'success' ) {
$('.error').html('<div class="alert alert-unknown">' + data.msg + '</div>');
// -> here
// correct setTimeout function
setTimeout(function(){
window.location = "user/profile.php";
},3000);
}
}
成功:函数(数据){
控制台日志(数据);
if(data.error){
$('.error').html(''+data.error.msg+'');
}
如果(data.result==='success'){
$('.error').html(''+data.msg+'');
//->这里
//正确设置超时功能
setTimeout(函数(){
window.location=“user/profile.php”;
},3000);
}
}
你是说302
,而不是304
?如果是这样,这是一个完全有效的响应,这是一个302错误。问题是页面没有使用ajax重定向。现在ajax部分工作正常,但是当JavaScript被禁用时,原始json会被打印出来。是的,没有JavaScript,ajax和Jquery就无法工作。如果你想让你的页面在没有JS的情况下也能正常工作,你必须做出如下改变:如果表单在没有JS的情况下发送,那么发送一个类似“ajax=0”的参数;如果JS正常工作,那么发送一个类似“ajax=1”的参数。在php文件中,检查“ajax”参数并决定要做什么。(直接发送Json或重定向)
success: function(data) {
console.log(data);
if (data.error) {
$('.error').html('<div class="alert alert-danger">' + data.error.msg + '</div>');
}
if ( data.result === 'success' ) {
$('.error').html('<div class="alert alert-unknown">' + data.msg + '</div>');
// -> here
// correct setTimeout function
setTimeout(function(){
window.location = "user/profile.php";
},3000);
}
}