weixin_33725270 2017-02-16 19:59 采纳率: 0%
浏览 28

PHP,JavaScript AJAX

I have the following code for a login-page:

function LoginToSite() {
    if (getElementById('username').value != "" && getElementById('password').value != "") {
        request.addEventListener('readystatechange', Login, false);

        var username = encodeURIComponent(document.getElementById("username").value);
        var password = encodeURIComponent(document.getElementById("password").value);


        request.open('GET', 'login.php?username='+username+"&password="+password, true);
        request.send(null);
    }
}

function Login() {
    if (request.readyState === 4 && request.status === 200) {
        alert("READY");
        var myResponse = JSON.parse(this.responseText);
        getElementById("count").innerHTML = myResponse;
        getElementById('login').style.display = "none";
        if(request.responseText == 1){
            alert("Login is successfull");
        }
        else if(request.responseText == 0){
            alert("Invalid Username or Password");
        }
    }
    else{
        alert("Error :Something went wrong");
    }
    request.send();
}

php-code

session_start();

$logins = array("username1" => "password1", "username2" => "password2" );

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
if($username != '' and $password != ''){

    foreach($logins as $key=>$value){
        if(($key == $username) && ($value == $password)){
            echo "1";
        }else{
            echo "0";
        }
    }

}else{
    echo "0";
}

When im trying to login, the site first alert that something went wrong, then the same thing happens again and after that, it alerts "ready". What do I have to change to get this right?

  • 写回答

2条回答 默认 最新

  • weixin_33713350 2017-02-16 20:08
    关注

    readystatechange event is called multiple times ... while the ready state changes from 0 -> 1 -> 2 -> 3 -> 4 ... all requests go through these changes ... your logic would alert an error when readystate becomes 1, then 2, then 3 ... only when readystate becomes 4 AND state is 200 will the "if" condition run

    you're alsoe calling request.send every time you go in to "Login"

    so a simple change

    function Login() {
        if (request.readyState === 4) {
            if (request.status === 200) {
                alert("READY");
                var myResponse = JSON.parse(this.responseText);
                getElementById("count").innerHTML = myResponse;
                getElementById('login').style.display = "none";
                if(request.responseText == "1"){
                    alert("Login is successfull");
                }
                else if(request.responseText == "0"){
                    alert("Invalid Username or Password");
                }
            }
            else{
                alert("Error :Something went wrong");
            }
        }
    }
    

    Note: checking request.responseText == "1" because the response will be a string not a number

    a better (in my opinion) solution is to NOT listen on readystate change, but listen for load event

    request.addEventListener('load', Login, false);
    

    then your Login code is

    function Login() {
        if (request.status === 200) {
            alert("READY");
            var myResponse = JSON.parse(this.responseText);
            getElementById("count").innerHTML = myResponse;
            getElementById('login').style.display = "none";
            if(request.responseText == "1"){
                alert("Login is successfull");
            }
            else if(request.responseText == "0"){
                alert("Invalid Username or Password");
            }
        }
        else{
            alert("Error :Something went wrong");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器