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 sayingtrue/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
2 Answers
Reset to default 6xmlhttp.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"