【发布时间】:2015-10-20 15:47:17
【问题描述】:
我使用 jQuery 通过$.post 在 PHP 文件中发送一个值。
$.post("ajax/comment_delete.php", {
comment_id: comment_id
}, function(response){/
if (response == true) {
tr.fadeOut(500, function() {
tr.remove();
})
}
})//$.post
在 PHP 文件中我有:
if (isset($_POST['comment_id'])) {
$comment_id = (int)$_POST['comment_id'];
$comment = comment::find_by_id($comment_id);
if (is_object($comment)) {
echo $comment->delete();
}
}
一切正常,除了当我使用response===true 时它不起作用。如果评论被删除,我的删除方法将返回 true。当我alert 回复时,它显示1; 1===true 为假正常吗?还是有什么问题?
【问题讨论】:
-
console.log("1"==true); console.log("1"===true);类型凝聚力 -
^ 这个。
===检查完全匹配,所以你基本上是在说 response 是否是 true 的布尔值,它永远不会是。==同时检查响应是否为truthy: -
@neilsimp 检查是否完全匹配,所以你基本上是在说 response 是否是 true 的布尔值,它永远不会是。为什么? php 发送 true 为 1?
-
我怀疑使用 == 因为它可能 php 发送错误字符串和字符串计数为 true
-
@peyman 你刚才说 php 发送 true 为 1,在 JavaScript 中
"1"与true不同,因为它们的类型不同。如果 PHP 发送"1"为 true 为什么不检查response === "1"
标签: javascript php jquery ajax type-coercion