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 数据中发送一个布尔值
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
