最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting a boolean value from xmlhttp.responseText - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question edited Nov 11, 2010 at 13:09 Quentin 945k133 gold badges1.3k silver badges1.4k bronze badges asked Nov 11, 2010 at 13:03 Hector BarbossaHector Barbossa 5,52813 gold badges50 silver badges70 bronze badges 2
  • What exactly is returned in xmlhttp.responseText? Is is a string saying true/false? A number? – Oded Commented Nov 11, 2010 at 13:06
  • This looks unfortunately like W3School's race condition prone XHR code. You need to work to eliminate the globals from it. – Quentin Commented Nov 11, 2010 at 13:10
Add a ment  | 

2 Answers 2

Reset to default 6

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

Perform a string parison.

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
}

try this:

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

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

发布评论

评论列表(0)

  1. 暂无评论