Hector Barbossa
Hector Barbossa

Reputation: 5528

Getting a boolean value from xmlhttp.responseText

I have my code like this for geetting the value of the variable isItemLocked.

 function authorItem(itemNumber){
    if (window.XMLHttpRequest)
                    {
                      xmlhttp=new XMLHttpRequest();
                    }else{
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    url ="Some URL";
                    xmlhttp.open("GET",url,true);
                    xmlhttp.send(null);
                    xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4) {
                        var isItemLocked = xmlhttp.responseText;
                        if(isItemLocked){
                            alert('Item has been Closed.Click OK to go to Search Page');
                            window.location = "SOME OTHER URL";
                        }else{
                            var url ="SOME OTHE URL 1";
                            location.href = url;    
                        }
                }
            }
 }

A returnning boolean value true for isItemLocked.But each time I am going to SOME OTHER URL.Any solutions?

Upvotes: 3

Views: 4483

Answers (2)

Quentin
Quentin

Reputation: 943214

xmlhttp.responseText doesn't return a boolean, it returns a string and "false" is true.

Perform a string comparison.

if (isItemLocked === 'true') {
    // Do one thing
} else if (isItemLocked === 'false') {
    // Do a different thing
} else {
    // You have an unexpected response from the server and should handle the error
}

Upvotes: 6

hunter
hunter

Reputation: 63512

try this:

var isItemLocked = xmlhttp.responseText.toString().toLowerCase() == "true";

The responseText is coming back as a string so you need to check if it is equal to the string "true"

Upvotes: 1

Related Questions