【发布时间】:2020-08-23 21:26:15
【问题描述】:
在验证用户凭据后,我尝试从登录页面重定向到主页。响应运行良好。在获得成功响应后,我想重定向到主页。我在客户端使用 Javascript,在服务器端使用 PHP。但成功响应没有到达 ajax。
Ajax 调用
$('#login-btn').click(function(e) {
if ($('#login-form')[0].checkValidity()) {
e.preventDefault();
$("#login-btn").html('<img src="images/tra.gif" /> Please Wailt..');
if ($('#Lemail').val() == '') {
$('#error-message').fadeIn().html('* Email is Required!');
setTimeout(function() {
$('#error-message').fadeOut('slow');
}, 5000);
} else if ($('#Lpassword').val() == '') {
$('#error-message').fadeIn().html('* Please enter password!');
setTimeout(function() {
$('#error-message').fadeOut('slow');
}, 5000);
} else {
$('#error-message').text('');
$.ajax({
url: 'actionlog.php',
method: 'POST',
data: $('#login-form').serialize() + '&action=login',
success: function(response) {
if (response === 'true') {
window.location = '../';
} else {
$('#logAlert').html(response);
}
}
});
}
}
});
});
PHP 代码:
<?php
//Login Request
if (isset($_POST['action']) && $_POST['action'] == 'login') {
if (Input::exists()) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'email' => array('required' => true),
'password' => array('required' => true)
));
if ($validation->passed()) {
$user = new User();
//create Remember
$remember = (Input::get('remember') === 'on') ? true : false;
$login = $user->login(Input::get('email'), Input::get('password'), $remember);
if ($login) {
echo 'true';
}
}else{
foreach ($validation->errors() as $error) {
$user = new User();
echo $user->showMessage('danger',$error);
}
}
}
}
我需要帮助
【问题讨论】:
-
需要更多调试细节。是否正在提出请求?您是否尝试过使用 ajax 错误处理程序?如果没有更多细节,我们只能猜测并问你很多问题