Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 743
Hector Barbossa Avatar asked Aug 10 '26 09:08

Hector Barbossa


1 Answers

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
}
like image 50
Quentin Avatar answered Aug 12 '26 23:08

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!