在AJAX请求中添加contentType
和processData
属性,将数据转换为JSON格式并发送。
JavaScript代码示例:
var data = {
empty1: [],
empty2: [],
filled: [1, 2, 3]
};
$.ajax({
type: 'POST',
url: 'your-php-file.php',
data: JSON.stringify(data),
contentType: 'application/json',
processData: false,
success: function(response) {
console.log(response);
}
});
PHP代码示例:
$data = json_decode(file_get_contents('php://input'), true);
$filledArray = $data['filled'];
// 处理 $filledArray