PHP返回空POST数组

时间:2014-03-18 09:24:57

标签: javascript php jquery

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
// (
// )

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

1 个答案:

答案 0 :(得分:3)

您将数据编码为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都是采用数据结构并将其表示为字符串的函数。使用其中一个,而不是两个。