Reputation: 475
My head is about to explode. I cannot figure out what is going on here. Below is my code for handling a ajax request.
xmlhttp_DELrep.onreadystatechange = function(){
if(xmlhttp_DELrep.readyState == 4 && xmlhttp_DELrep.status == 200){
if(xmlhttp_DELrep.responseText == 'delete_ok'){
document.getElementById(replyboxID).style.display='none';
console.log('delete success!');
}else{
console.log('delete fail');
console.log('xmlhttp_DELrep.responseText: '+ xmlhttp_DELrep.responseText);
}
}
}
This is the response that I am getting through the console:
delete fail
xmlhttp_DELrep.responseText: delete_ok
I do not understand why my first 'if' statement is not executing given that the xmlhttp_DELrep.responseText does indeed equal 'delete_ok' - as shown in the console. Please can someone help?
Upvotes: 0
Views: 291
Reputation: 147
I don't know if it would solve your issue, but i would try different ways of comparing the strings.
As found in the references below, you could use:
string_a.localeCompare(string_b);
/*
Returns:
0: exact match
-1: string_a < string_b
1: string_b > string_b
*/
Another issue could be white spaces that are not visible in console output.
if(xmlhttp_DELrep.responseText.trim() == 'delete_ok') { ... }
References:
https://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript
Upvotes: 0
Reputation:
You may have white spaces to the right of the text response, which you cannot see in your console output. Your code will work properly if you trim them away with trim()
.
Upvotes: 1