我试图比较两个似乎包含相同值但不起作用的字符串。JavaScript比较具有相同值的两个字符串失败
这里是我的代码:
ABC.prototype.is_in_DB = function() {
var ans = "";
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
ans = (xmlhttp.responseText);
}
return ans;
}
xmlhttp.open("POST", "ABC_DB.php", false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("id=" + this.get_id() + "&name=" + this.get_type() + "&description=" + this.get_description() + "&kind=" + this.get_kind());
var check = "true";
console.log("this is ans type " + typeof (ans));
console.log("this is ans length " + ans.length);
console.log("this is ans " + ans);
console.log("this is check type " + typeof (check));
console.log("this is check length " + check.length);
console.log("this is check " + check);
if (ans === check) {
console.log("the if is true ");
return true;
} else {
console.log("the if is false ");
return false;
}
}
php文件的作品,它是结束看起来像这样(有文件中没有更多的回声命令)
if ($row_cnt>0){
echo('true');
} else {
echo('false');
}
当我打印我收到以下字符串的值
这是ans类型字符串
这是ANS长度5
这是ANS真
这是检查类型字符串
这是支票长度4
这是检查真
的如果是假
你有什么想法,我怎么能比较一下?
看起来像ANS包含响应一些白色的空间,因此使用'ans.trim()== check' –
你为什么这样做同步AJAX? Eeeck。在ajax调用期间锁定浏览器。 – jfriend00