[英]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.