2

I am sending a post request to a PHP script via AJAX like so:

$.post("login.php", {u: username,p: password})
    .done(function(response){
        console.debug(response);
        if (response === "0") {
            $("#loginMessage").html("Invalid Username or Password");
        } else {
            $.mobile.changePage("#homePage");
        }
})

And login.php snippet. If the login credential fails, I am echoing "0". I believe this would output a string, since it is in quotes, as opposed to echo 0;

    if ($stmt->num_rows > 0 && password_verify($password,$hashedPassword)) {
        $_SESSION["uid"] = $uid;
    } else {
        echo "0";
    }

However, when I enter invalid credentials the comparison response === "0" is returning FALSE, although when I use console.debug(response), it is showing that it is indeed 0.

After some of my own fiddling, I found that if I change response === "0" to response == 0, it correctly evaluates to true.

response === 0 also evaluates to FALSE, which confuses me even more.

Can someone explain this a bit for me?

9
  • 1
    make sure that (for whatever reason), js isn't converting that 0 from the ajax response into an integer. 0 === "0" is false in JS, because the types don't match (string v.s. integer). Commented Jun 17, 2015 at 14:46
  • 1
    That is because === checks for equality in value but also in the type of the variable. Commented Jun 17, 2015 at 14:46
  • What does console.log(response.length) tell you? Commented Jun 17, 2015 at 14:46
  • Also, when I changed echo "0" to echo "error"and changed my string comparison to compare response and the string "error", like so: if (response === "error") it would return false, although console.debug(response) would show the string "error". Very confusing :( Commented Jun 17, 2015 at 14:47
  • 1
    If you have any whitespaces outside the <?php ... ?> delimiters, they will be included in the response. I assume the response you get is actually 0\n or something similar. Commented Jun 17, 2015 at 14:48

2 Answers 2

1

A typical reason for this problem is that the response contains whitespace characters. They wouldn't necessarily be obvious in the console, but inspecting response.length is one easy way to find out.

Keep in mind that anything outside the <?php ... ?> delimiters, also whitespace characters (e.g. line breaks), will be included in the response.

A typical way to prevent trailing whitespace characters is to not have a closing ?> at all.


While you could simply "trim" the response before the comparison, the better solution is to fix the server side script to really just send what you want to send.

Sign up to request clarification or add additional context in comments.

Comments

0

Strict equality Operator compares both value and data type. For example, if you compare zero as a string and zero as a number, it returns false. so that is why you see false. Make sure you compare 2 objects with the same type and data when you what to use "Strict equality" (===) otherwise use simple Equality operator (==).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.