从 xmlhttp.responseText 获取布尔值

Hec*_*ssa 2 javascript ajax

我有这样的代码来获取变量 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;    
                        }
                }
            }
 }
Run Code Online (Sandbox Code Playgroud)

isItemLocked 的返回布尔值为 true。但每次我要去其他一些 URL 时。有任何解决方案吗?

Que*_*tin 5

xmlhttp.responseText不返回布尔值,它返回一个字符串并且"false"true.

执行字符串比较。

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
}
Run Code Online (Sandbox Code Playgroud)