

新闻资讯
技术教程PHP接收Ajax数据需据Content-Type区分处理:JSON需读php://input并json_decode;FormData走$_POST/$_FILES;默认x-www-form-urlencoded直接用$_POST。
Ajax提交表单时,$_POST 不一定能直接拿到数据——关键看前端用什么方式发的。如果前端用 fetch 或 XMLHttpRequest 发的是 application/json,那 PHP 默认根本不会解析进 $_POST,必须手动读取原始输入。
FormData 提交(含文件或普通字段)→ 数据在 $_POST 和 $_FILES 中可用JSON.stringify() 发送 → 必须用 file_get_contents('php://input') 读取,再 json_decode()
jQuery.ajax() 且没设 contentType → 默认是 application/x-www-form-urlencoded,走 $_POST
if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
exit('Invalid JSON');
}
} else {
$data = $_POST;
}
常见错误:前端发了 JSON,但 PHP 脚本只写 print_r($_POST),结果空数组。这不是 PHP 问题,是协议不匹配。
Content-Type: application/json → PHP 不自动解析,$_POST 永远为空Content-Type: multipart/form-data → 只有 FormData 且含文件时才触发,此时 $_POST 仍可读普通字段Content-Type 缺失或为 text/plain → php://input 可读,但需自行解析,$_POST 无效用 FormData 是上传文件唯一可靠方式,但后端容易忽略两点:一是 $_FILES 键名对应前端 append() 的第一个参数;二是 PHP 配置限制(如 upload_max_filesize)会静默失败。
formData.append('avatar', fileInput.files[0]) → 后端用 $_FILES['avatar']
$_FILES['avatar']['error'] === UPLOAD_ERR_OK,别只看是否非空move_uploaded_file($_FILES['avatar']['tmp_name'], $dest),不能用 copy()
$targetDir = __DIR__ . '/uploads/';
if (!is_dir($targetDir)) mkdir($targetDir, 0755, true);
$fileName = uniqid() . '_' . basename($_FILES['avatar']['name']);
$targetPath = $targetDir . $fileName;
if ($_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
if (move_uploaded_file($_FILES['avatar']['tmp_name'], $targetPath)) {
echo json_encode(['status' => 'success', 'path' => $targetPath]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to save file']);
}
} else {
http_response_code(400);
echo json_encode(['error' => 'Upload error: ' . $_FILES['avatar']['error']]);
}
Ajax请求和普通表单一样可被任意篡改,$_POST 或 json_decode 出来的数据必须当作不可信输入处理。
filter_input() 或 filter_var() 校验类型(如邮箱、整数),别只用 isset() 或 empty()
htmlspecialchars(),尤其当数据可能回显到页面error_log(print_r($_SERVER, true)); error_log(print_r($_POST, true)); error_log(file_get_contents('php://input'));,比猜快得多。