0

I'm running an Ajax call which has a success function which takes in a variable returned from the PHP page like so.

Ajax:

$.ajax ({
    type: "POST",
    url: "loginrequest.php",
      data: 'username=' + username + '&password=' +pass,
    success: function(html){
        console.log(html); // Returns login
        console.log(typeof html); // Returns string
        console.log(html === "login"); // Returns false

        if(html === 'login'){
            window.location.href = 'index.php';
        }
        else if(html === 'false'){
            alert("login failed");
        }
    }
});

PHP:

if($count == 1){
    $_SESSION['user'] = $myusername;
    $return = "login";
    echo json_encode($return);
}
else {
    $return = "false";
    echo json_encode($return);
}

As you can see, I'm trying to implement a simple login page and then redirect the user or display an alert depending on the outcome of the number of rows returned from my database query.

What I don't understand is this:

console.log(html); // Returns "login"
console.log(typeof html); // Returns string
console.log(html === "login"); // Returns false

I tried echo-ing without json_encode() and it still would give me the same results. I was using ==, but then I read that it's safer to use ===, so I switched to that, but it still won't return true.

3
  • 2
    To fix the immediate issue, try console.log(html.trim() === "login");. trim() removes the additional whitespace which can be appended to the response when dealing with plain text. As an improvement, return JSON instead to avoid the problem entirely. Commented Jul 4, 2016 at 14:22
  • 2
    json_encode adds " to the string, so you actually have "login" with the quotes. Take a look: https://eval.in/600312 Commented Jul 4, 2016 at 14:22
  • 2
    A quick var_dump of the generated 'json' would've easily showed the problem ;) Commented Jul 4, 2016 at 14:33

2 Answers 2

4

You're sending JSON, which means that you're sending the literal bytes:

"login"
"false"

Note the quotes in there. Your JavaScript code either needs to decode the JSON, or compare the raw JSON itself:

 result = JSON.parse(html)
 if (result == "login")

or

 if (html == '"login"')   // Note the quotes

A simple console.log(html) would have shown you what you're dealing with.

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

2 Comments

From the question, the op actually did run console.log(html); // returns "login", but they probably thought it was how the console displays strings... xD
json is a string. plain and simple. that's the whole point of it. encoding arbitrary JS data structures/types into a simple string for transport. raw json received via http will always be a string.
1

If you are using json_encode on the PHP side then you should use:

jQuery.parseJSON on the JavaScript side -

html = jQuery.parseJSON(html);

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.