Send a boolean value in jQuery ajax data(在 jQuery ajax 数据中发送一个布尔值)
问题描述
我在 Ajax 调用中发送一些数据.其中一个值是设置为 FALSE 的布尔值.在 Ajax 调用的 PHP 脚本中,它总是被评估为 TRUE.有任何想法吗?
I'm sending some data in an Ajax call. One of the values is a boolean set to FALSE. It is always evaluated as TRUE in the PHP script called by the Ajax. Any ideas?
$.ajax({
type: "POST",
data: {photo_id: photo_id,
vote: 1,
undo_vote: false}, // This is the important boolean!
url: "../../build/ajaxes/vote.php",
success: function(data){
console.log(data);
}
});
在上面Ajax中调用的脚本vote.php中,我检查了布尔值:
In vote.php, the script that is called in the above Ajax, I check the boolean value:
if ($_POST['undo_vote'] == true) {
Photo::undo_vote($_POST['photo_id']);
} else {
Photo::vote($_POST['photo_id'], $_POST['vote']);
}
但总是满足 $_POST['undo_vote'] == true
条件.
推荐答案
帖子只是文本,而文本在 php 中将评估为 true.一个快速的解决方法是发送一个零而不是错误.你也可以在 PHP 中为你的 true 加上引号.
A post is just text, and text will evaluate as true in php. A quick fix would be to send a zero instead of false. You could also put quotes around your true in PHP.
if ($_POST['undo_vote'] == "true") {
Photo::undo_vote($_POST['photo_id']);
} else {
Photo::vote($_POST['photo_id'], $_POST['vote']);
}
然后你可以传入真/假文本.如果那是你喜欢的.
Then you can pass in true/false text. If that's what you prefer.
这篇关于在 jQuery ajax 数据中发送一个布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jQuery ajax 数据中发送一个布尔值


- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01