0

I'm trying to compare two strings which seems containing the same value but it doesn't work.

here is my code:

ABC.prototype.is_in_DB = function () {
    var ans = "";
    if (window.XMLHttpRequest) {
        var xmlhttp = new XMLHttpRequest();
    } else {
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            ans = (xmlhttp.responseText);
        }
        return ans;
    }
    xmlhttp.open("POST", "ABC_DB.php", false);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send("id=" + this.get_id() + "&name=" + this.get_type() + "&description=" + this.get_description() + "&kind=" + this.get_kind());

    var check = "true";

    console.log("this is ans type    " + typeof (ans));
    console.log("this is ans length    " + ans.length);
    console.log("this is ans     " + ans);
    console.log("this is check type    " + typeof (check));
    console.log("this is check length    " + check.length);
    console.log("this is check     " + check);

    if (ans === check) {
        console.log("the if is true ");
        return true;
    } else {
        console.log("the if is false ");
        return false;
    }
}

the php file works and it's end looks like this (there are no more echo commands in the file)

 if ($row_cnt>0){
     echo('true');
 } else {
     echo('false');
 } 

when I'm printing the values of the strings i receive the following

this is ans type string

this is ans length 5

this is ans true

this is check type string

this is check length 4

this is check true

the if is false

Do you have any idea how I can compare it?

2
  • looks like ans contains some white space from response so use ans.trim() == check Commented Apr 4, 2014 at 20:34
  • Why are you doing synchronous ajax? Eeeck. Lock up the browser during the ajax call. Commented Apr 4, 2014 at 20:36

5 Answers 5

1

Try to do ans.trim() and using == instead of === It seems that ans could also have a trailing character like a \n

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

Comments

0

Wild guess here, but because the length of "ans" is 5, but the value is "true", I'm willing to bet there is a space in there. Try adding trim(ans) before your echo's, and see if that fixes it.

Comments

0

It seems your responseText is coming with some white spaces.

Try to use: if (ans.trim() == check)

Comments

0

try eliminate the possibility that ans and check are not the same object use == instead ===

Comments

0

thanks to everybody. what i found that works for me was to add this two following line

x=(xmlhttp.responseText);
ans=x.replace(/^\s*/,'').replace(/\s*$/,'').toLowerCase();

the problem i faced was due to additional white spaces. this line eliminate them.

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.