如果声明不工作 - javascript

时间:2016-04-24 20:26:54

标签: javascript php if-statement

我无法识别此javascript代码中的问题。我在某些逻辑上从服务器返回代码154,155和1。这些数字是返回但是如果语句在if语句中返回一个值,则它仍然无法正常工作它仍然运行else(默认)。不知道为什么它不能检查值是否相同。

`$("#btnlogin").click(function (){

   // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "phpfiles/login.php";
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;    

    var vars = "username="+username+"&password="+password;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "plain/text");

    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;


            if (return_data == "154"){
                $.Notification.notify('error','top left', 'Wrong Information', 'Username/Password is incorrect');
            }else if (return_data=== "155")
            {
                $.Notification.notify('error','top left', 'User Does not exist', 'This user Does not exist. Please Check and try again.');
            }
            else if (return_data == "1")
            {
                $.Notification.notify('success','top left', 'Success', '');
                GenToken();
            }else{
                alert();
            }
        };
    };
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request




});`

无论条件是真还是假,它总是归结为别的。它指向这段代码

`<?php

if(isset($_POST['username']))
{
    $username = FILTER_INPUT(INPUT_POST, "username", FILTER_SANITIZE_STRING);
    $password = FILTER_INPUT(INPUT_POST, "password", FILTER_SANITIZE_STRING);

    require_once("../class/class.login.php");
    $lh = new LoginHander;

    $lh->LogUserIn($username, $password, 'admin');
}
?>`

这是调用此函数

`    public function LogUserIn($username, $password)
    {
        $username = $this->es($username);
        $password = $this->es($password);
        $ip = $this->getClientIP();
        $sql = "SELECT username, password FROM admin WHERE username = ?";

        try{
            $query = $this->con->prepare($sql);
            $query->execute(array($username));
            $result = $query->fetchall(PDO::FETCH_ASSOC);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->addlog($username, $ip);
        if(count($result) > 0)
        {
            header("Content-type: plain/text");
           $pass = $result[0]['password'];
           if(password_verify($password, $pass)){
               $this->SetVars($username);
               echo "1";
           }else{
                echo "154";
                exit();
           }
        }
        else
        {
           echo "155";
                exit();
        }
    }`

我不知道我尝试过===而不是==的问题在哪里,但它没有帮助。请告诉我我犯了哪个错误。提前谢谢

1 个答案:

答案 0 :(得分:0)

可以在浏览器中调试Javascript。每当观察到一个条件没有被正确评估时,建议用调试器进行检查。

return_data == "154"

是非严格检查,即使其类型不同,也会评估为true154 == "154"true。另一方面

return_data=== "155"

是严格的比较。 155 === "155"false

return_data == "1"

是另一种非严格的比较。

通常,这些错误发生在对类似但不相同的值使用严格比较的情况下,或者在值的开头或结尾处可以找到白色字符时。事实证明,情况确实如此。