Javascript 验证用户是否已登录的正确方法
我使用AJAX请求创建网站的用户区域。 只有当用户连接时,才能显示“注销”按钮 为了连接用户,这里是我的AJAX请求:Javascript 验证用户是否已登录的正确方法,javascript,ajax,Javascript,Ajax,我使用AJAX请求创建网站的用户区域。 只有当用户连接时,才能显示“注销”按钮 为了连接用户,这里是我的AJAX请求: function requestSI() { var xhr = getXMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { document.
function requestSI() {
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
document.getElementById("popup_login").innerHTML = "<img src='img/index/cross.png' id='close' onclick ='hidelogin()' /><div id='color2'></div><h2 id=login_title>";
document.getElementById("popup_login").innerHTML += this.responseText;
if (isUserLogged()){
var x = document.getElementById("log_off");
x.classList.remove("inative_logoff");
x.classList.add("active_logoff");
}
}
};
var login_fieldSI = encodeURIComponent(document.getElementById("login_fieldSI").value);
var password_fieldSI = encodeURIComponent(document.getElementById("password_fieldSI").value);
xhr.open("POST", "php/login.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("Login_fieldSI="+login_fieldSI+"&Password_fieldSI="+password_fieldSI);
}
由于某些原因,我的“注销”按钮没有显示。您可以尝试如果(isUserLogged()==true){
而不是如果(isUserLogged()){
函数请求i(){
var xhr=getXMLHttpRequest();
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&(xhr.status==200 | | xhr.status==0)){
document.getElementById(“popup_login”).innerHTML=“”;
document.getElementById(“popup_login”).innerHTML+=this.responseText;
isUserLogged();
}
};
var login_fieldSI=encodeURIComponent(document.getElementById(“login_fieldSI”).value);
var password\u fieldSI=encodeURIComponent(document.getElementById(“password\u fieldSI”).value);
open(“POST”,“php/login.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.send(“Login_fieldSI=“+Login_fieldSI+””和Password_fieldSI=“+Password_fieldSI”);
}
----------
函数isUserLogged(){
var xhr=getXMLHttpRequest();
xhr.onreadystatechange=函数(){
如果(xhr.readyState==4&&(xhr.status==200 | | xhr.status==0)){
警报(this.responseText);
如果(this.responseText==“true”){
警报(“正常”);
警报(“测试功能”);
var x=document.getElementById(“注销”);
x、 类列表。删除(“取消登录”);
x、 添加(“主动退出”);
}
否则{
//执行其他代码。。
}
}
};
open(“POST”,“php/is\u user\u connected.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.send(空);
}
if(isUserLogged()==true){…….它显示什么?->未定义……但此警报显示在警报(this.responseText)和警报('ok')之前这是关于sync和asnyc的问题…………请阅读此链接…………是的,我正在考虑……但我真的不知道如何在我的情况下执行回调函数……可能重复
function isUserLogged(){
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
alert(this.responseText);
if (this.responseText == "true") return true;
else return false;
}
};
xhr.open("POST", "php/is_user_connected.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(null);
}
function requestSI() {
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
document.getElementById("popup_login").innerHTML = "<img src='img/index/cross.png' id='close' onclick ='hidelogin()' /><div id='color2'></div><h2 id=login_title>";
document.getElementById("popup_login").innerHTML += this.responseText;
isUserLogged();
}
};
var login_fieldSI = encodeURIComponent(document.getElementById("login_fieldSI").value);
var password_fieldSI = encodeURIComponent(document.getElementById("password_fieldSI").value);
xhr.open("POST", "php/login.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("Login_fieldSI="+login_fieldSI+"&Password_fieldSI="+password_fieldSI);
}
----------
function isUserLogged(){
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
alert(this.responseText);
if (this.responseText == "true"){
alert("ok");
alert("test function");
var x = document.getElementById("log_off");
x.classList.remove("inative_logoff");
x.classList.add("active_logoff");
}
else {
//do your other code..
}
}
};
xhr.open("POST", "php/is_user_connected.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(null);
}