[英]PHP returning empty POST array
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支持的标准格式之一对其进行编码。
或者,请参阅此问题以了解如何获取原始请求正文。
也:
echo json_encode(print_r($_POST, true));
json_encode
和print_r
都是采用数据结构并将其表示为字符串的函数。 使用其中一个,而不是两个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.