如何解决TimeoutAndRedirect 未在 onload() 中定义
我正在尝试通过 AJAX 登录,但看起来我不能这样做......基本上,我想要做的是,根据用户类型,登录后,我应该被重定向到某个页面。
调用如下所示:<body onload="TimeoutAndRedirect('student')">
有时我可以登录,有时我不能,我不明白为什么...这是我的 timeoutandredirect 函数,我最初使用了 cookie 和 XHR 请求,但它太慢了:
function TimeoutAndRedirect(Type) {
$.ajax
({
type: "GET",url: "../PHP/redirect.PHP",success: function(respond)
{
if (respond == "not logged.") {
alert("Your login period has expired! Please login again!");
window.location.href = "../login.html";
}
else if (respond != Type) {
alert("You cannot access this page using your account!");
if (respond == "admin") {
window.location.href = "../page/admin-system-management.PHP";
} else if (respond == "student"){
window.location.href = "../page/student-dashboard.PHP";
} else if (respond == "teacher"){
window.location.href = "../page/teacher-dashboard.PHP";
}
}
}
}
// var Request = new XMLHttpRequest();
// Request.open("GET","../PHP/redirect.PHP",true);
// Request.send();
// Request.onload = function() {
// var respond = Request.responseText;
// if (respond == "not logged.") {
// alert("Your login period has expired! Please login again!");
// window.location.href = "../login.html";
// } else if (respond != Type) {
// alert("You cannot access this page using your account!");
// if (respond == "admin") {
// window.location.href = "../page/admin-system-management.PHP";
// } else if (respond == "student"){
// window.location.href = "../page/student-dashboard.PHP";
// } else if (respond == "teacher"){
// window.location.href = "../page/teacher-dashboard.PHP";
// }
// }
// }
}
这是我的 checklogin.js 文件:
function login() {
var enterID = document.getElementById("enterID").value;
var password = document.getElementById("password").value;
if ((password != "") && (enterID != "")) {
var info = "?enterID=" + enterID + "&password=" + password;
$.ajax
({
type: "GET",url: "PHP/login.PHP"+info,success: function(respond)
{
if (respond == "admin") {
window.location.href = "page/admin-system-management.PHP";
} else if (respond == "student"){
window.location.href = "page/student-dashboard.PHP";
} else if (respond == "teacher"){
window.location.href = "page/teacher-dashboard.PHP";
} else{
document.getElementById("errorMessage").innerText = respond;
}
}
});
}
else {
document.getElementById("errorMessage").innerText = "Please fill in all the fields.";
}
}
function redirect() {
$.ajax
({
type: "GET",url: "PHP/redirect.PHP",success: function(respond)
{
if (respond != "not logged.") {
if (respond == "admin") {
window.location.href = "page/admin-system-management.PHP";
} else if (respond == "student"){
window.location.href = "page/student-dashboard.PHP";
} else if (respond == "teacher"){
window.location.href = "page/teacher-dashboard.PHP";
}
}
}
});
}
<?PHP
session_start();
include "mysql-connect.PHP";
//get Info from login.html
$ID = $_GET['enterID'];
$PW = $_GET['password'];
$stmt = $connect->prepare("SELECT PW,userType,nickName FROM users WHERE ID = ?");
$stmt->bind_param("s",$ID);
$valid = $stmt->execute();
if (!$valid){
die("Could not successfully run query.". $connect->connect_error);
}
$result = $stmt->get_result();
if ($result->num_rows==0){
//display message of no such student/teacher/admin
echo "Failed to find an account with the input ID.";
} else {
$row = $result->fetch_assoc();
if ($PW == $row['PW']) {
$type = $row['userType'];
$nick = $row['nickName'];
//save data,record cookie for 6hours
$_SESSION["type"]=$type;
$_SESSION["userID"]=$ID;
$_SESSION["nickName"]=$nick;
//login success - Request.responseText to checklogin.js
echo $type;
} else {
//display message of password error
echo "The input password does not match the account password.";
}
}
$connect->close();
?>
我做错了什么? 有时我也遇到了,,XHR加载GET失败”,然后我断开连接,然后我再次尝试登录,我需要再等几秒钟才能尝试登录,我不明白为什么..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。