验证用户是否已记录的正确方法

时间:2017-06-15 10:45:52

标签: javascript ajax

我使用AJAX请求来创建我网站的用户区域。 只有在用户连接时才能显示“注销”按钮。

为了连接用户,这里是我的AJAX请求:

this._browser = this.inAppBrowser.create('http://www.google.com');
    this._browser.on('loadstart').subscribe((event) => {
       let alert1 = this._alertCtrl.create({
        subTitle: 'load start',
        buttons: ['ok!']
      });
      alert1.present();
    });

这是我的AJAX请求,用于检查用户是否已连接:

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);
}

由于某种原因,我的“注销”按钮无法显示。

2 个答案:

答案 0 :(得分:0)

     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);
    }

答案 1 :(得分:-1)

您可以尝试if (isUserLogged() == true){而不是if (isUserLogged()){