您好我正在为我的网页制作一个jquery ajax解决方案,有一个getir.php 我将它用于jquery发布数据(它是一个用户名),如果名称存在,则回显'TRUE'或'FALSE'
这里是
if (isset($_POST['name'])) {
if($synan->checkNameIsExist($_POST['name'])){
echo'TRUE';
}else{
echo'FALSE';
}
}
它运行正常我在控制台中看到了真或假结果,但我在jquery part
上遇到了问题$(document).ready(function () {
$("#user_name_reg").blur(function () {
txt = $(this).val();
$.post("getir.php", {
name: txt,
method: "checkName"
}, function (result) {
console.log(result);
if (result == 'TRUE') {
$("span").html("answer true");
} else {
$("span").html("answer false");
}
});
});
});
我无法使其成为if语句。 谢谢你的建议
答案 0 :(得分:3)
鉴于您的输出来自:
console.log(typeof result, result.length, result);
是string 78 true
,我们知道3件事:
以下解决方案可以解释这些观察结果:
result = $.trim(result).toLowerCase();
if (result == 'true') {
$("span").html("answer true");
} else {
$("span").html("answer false");
}
答案 1 :(得分:1)
如果PHP使用默认内容类型:'text / plain'并且实际上只返回'TRUE'或'FALSE'而没有任何前导空格或其他内容上面的示例<强>将工作。在调用console.log()
时,您应该使用输出周围的引号来确保这一点。像这样:
console.log("'" . result . "'");
如果周围有空格,请确保在开始<?php
标记之前或在PHP中关闭?>
标记之后没有内容