javascript - JS 用 if 检查返回函数,总是返回 false

标签 javascript html if-statement

我想制作一个简单的网站,需要登录,如果登录是某个字符串(例如 Andrew),我希望它在 JS 提示符下询问密码。输入密码后,ajax 函数会检查简单的 php 文件中的密码。

为了更好地理解事情,这里是代码:

function login()
            {
                if (document.getElementById("username").value == "" || document.getElementById("username").value == null)
                {
                    alert("Wrong username... actually empty username :)\nTry again.");
                    return;
                }
                if (document.getElementById("username").value == "andrew")
                {
                    var pass = prompt("Password : ", "");
                    if (pass == "")
                    {
                        alert("Your password is empty!\nYou fucked it or you're not really Andrew!");
                        return;
                    } 

                    //THIS IF IS ALWAYS GOING IN ELSE

                    if (check_pass(pass))
                    {
                        alert("good password");
                        return;
                    }
                    else
                    {
                        alert("wrong");
                        return;
                    }
                }
                setCookie("username", document.getElementById("username").value, 365);
                window.location.href = "chat.php";
            }

这是我单击登录按钮后调用的函数。

这是我用来检查密码是否正确的check_pass 函数(此函数有效,但仅供引用):

  function check_pass(password)
        {
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    var checker = xmlhttp.responseText;
                    //alert(checker);
                    if (checker.indexOf('1') !== -1)
                    {
                        //alert("in true");
                        return true;
                    }
                    else
                    {
                        //alert("in false");
                        return false;
                    }
                }
            }
            xmlhttp.open("POST", "checkpass.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("password=" + password);
        }

在 check_pass 函数中,我放置了一些警报来测试我是否确实获得了正确的值,是的,如果响应为 1,则应返回 true,如果响应为 0,则应返回 false。它确实做到了这一点。但是...在登录函数中,这部分代码:

    if (check_pass(pass))
            {
                alert("good password");
                return;
            }
            else
            {
                alert("wrong");
                return;
            }

始终返回 false。

我已经这样做了 1 个小时,但我看不出出了什么问题。有任何想法吗 ?我还尝试过 check_pass(pass) === truecheck_pass(pass) == true 但它总是返回 false。

如有任何建议,我们将不胜感激。

最佳答案

AJAX 是异步的——您需要一个回调函数。修改您的 check_pass 调用以使用 result 参数进行回调

check_pass(pass, function(result) {
    //do stuff with your result!
}

并修改您的 AJAX 以使用数据执行回调:

function check_pass(password, callback) {
    ....
    xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                var checker = xmlhttp.responseText;
                //alert(checker);
                if (checker.indexOf('1') !== -1)
                {
                    //alert("in true");
                    callback(true);
                }
                else
                {
                    //alert("in false");
                    callback(false);
                }
            }
        }
}

关于javascript - JS 用 if 检查返回函数,总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759080/

相关文章:

html - 如何选择除特定 div 内的元素之外的所有元素

html - align = "center"不适用于表格

if-statement - 在Dart中如何使用true的返回值?

javascript - 为什么这个函数只返回零和一?欧拉项目 JavaScript

javascript - 上一页带有 ui-router 的页面

javascript - 如何在两个字体系列之间切换

html - 如何相对于 flexbox 祖 parent 垂直居中 div?

c++ - 赞成/反对 : Initializing a variable in a conditional statement

linux - 当没有用户登录时远程关闭Linux计算机

javascript - 三.js透明纹理和着色器 Material