通过 Ajax(从客户端)发送的 bool 值在服务器端变成字符串:
var ban_status = null;
ban_status = true;
$.ajax({
type: 'POST',
url: app.baseUrl + "/admin/users/api-ban-user",
data: { "userId": user_id, "banStatus": ban_status },
datatype: "json",
success: function (response) {
if (response.status === true) {
addAlert(response.msg, 'success');
userList();
} else {
addAlert(response.msg, 'error');
}
}
});
在 PHP 中
$banStatus = $post['banStatus'];
gettype($post['banStatus'])
返回字符串。如何返回 bool 值。
最佳答案
如前所述;你可以使用 json_decode在 php 中,但由于发布数据是一个字符串,您可以发送一个名为 json 的参数并在 JavaScript 中对您的对象进行字符串化:
var ban_status = null;
ban_status = true;
$.ajax({
type: 'POST',
url: app.baseUrl + "/admin/users/api-ban-user",
data: {json:JSON.stringify({ "userId": user_id, "banStatus": ban_status })},
datatype: "json"
}).then(
function (response) {
if (response.status === true) {
addAlert(response.msg, 'success');
userList();
} else {
addAlert(response.msg, 'error');
}
}
).fail(//use .catch if you have a new enough jQuery
function(err){
console.warn("something went wrong:",err);
}
);
在 PHP 中:
$postedObject = json_decode($post['json']);
$banStatus = $postedObject->banStatus;
关于javascript - 通过 Ajax 调用发送 bool 值时,PHP 将值作为字符串获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879745/