gpt4 book ai didi

javascript - PHP 返回空的 POST 数组

转载 作者:行者123 更新时间:2023-11-30 12:46:00 24 4
gpt4 key购买 nike

JavaScript jQuery 帖子:

var animals = {
animalsArray: [],
tripId: <?php echo $_GET['id']; ?>
};

$('.student-check').each(function() {
animals.animalsArray.push($(this).attr('id'));
});

var sendMe = JSON.stringify(animals);

$.post("saveChanges.php", sendMe, function(response) {
console.log(response);
}, "json");

PHP 处理程序:

echo json_encode(print_r($_POST, true));
// Array
// (
// )

为什么数组是空的?我正在发布数据,但响应返回一个空数组。

最佳答案

您将数据编码为 JSON,这不是 PHP 原生处理表单提交的格式。

删除 var sendMe = JSON.stringify(animals); 并将 animals 传递给 $.post 而不是 sendMe。 jQuery 将使用 HTML 表单和 PHP 支持的一种标准格式对其进行编码。

或者参见 this question了解如何获取原始请求正文。


还有:

echo json_encode(print_r($_POST, true));

json_encodeprint_r 都是采用数据结构并将其表示为字符串的函数。使用其中之一,而不是两者。

关于javascript - PHP 返回空的 POST 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22475032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com