AJAX发送表单数据后PHP端$_POST数组为空的问题求助
Hey there, let's break down why your PHP $_POST array is coming up empty, even though you can see valid JSON data in your frontend console.
The Core Issue
When you set contentType: "application/json" in your jQuery AJAX call, you’re telling the browser to send the request body as a raw JSON string. PHP’s $_POST superglobal only automatically parses data sent in application/x-www-form-urlencoded or multipart/form-data formats—it doesn’t handle raw JSON by default. That’s why print_r($_POST) shows an empty array.
Solution 1: Read Raw Request Data in PHP (Keep JSON Format)
If you want to keep sending JSON from the frontend, modify your PHP code to read the raw request body and convert it into a usable associative array. Note that serializeArray() outputs an array of name/value pairs, so we’ll need to reformat that into a key-value structure:
<?php header("Content-Type: application/json", true); // Set proper CORS headers on the server (not frontend) header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: Content-Type, Origin, Accept, X-Requested-With"); require_once("../../db.php"); // Read raw JSON from the request body $rawInput = file_get_contents("php://input"); // Parse JSON into a PHP array $formDataArray = json_decode($rawInput, true); // Convert the [{"name":"key", "value":"val"}] format to an associative array $postData = []; foreach ($formDataArray as $item) { $postData[$item['name']] = $item['value']; } // Now use $postData like you would use $_POST (e.g., $postData['uname']) echo json_encode($postData); ?>
Quick Notes:
- I moved the CORS headers to the PHP side—setting them in the frontend request does nothing; CORS rules are enforced by the server.
- The
json_decode($rawInput, true)call ensures we get an associative array instead of a PHP object. - The loop cleans up the
name/valuearray into a structure that matches what you’d expect from$_POST.
Solution 2: Send Form-Encoded Data (Simpler for Login Forms)
If you don’t specifically need JSON, adjust your frontend code to send data in the standard form-encoded format that PHP automatically parses into $_POST:
function attemptLogin(form){ // Serialize form directly into url-encoded format (no JSON conversion) var formData = $('#loginForm').serialize(); var url = "http://www.rsbmat.com/api/login.php"; console.log(formData); // Will look like: affiliate=South+King+County&uname=testuser&password=testpassword $.ajax({ headers: {'Access-Control-Allow-Headers': "Content-Type, Origin, Accept, X-Requested-With"}, method: "POST", url: url, cache: false, data: formData, crossDomain: true, dataType: "json", beforeSend: function(xhr){ xhr.withCredentials = true; } }).done(function(data){ console.log(data); }); }
With this change, your original PHP code using echo json_encode($_POST); will work perfectly—PHP will automatically parse the form-encoded data into the $_POST array.
Bonus: Fixing CORS
A quick reminder: The Access-Control-Allow-Origin header must be set on the server (your PHP file), not in the frontend request. Browsers enforce CORS rules, so the frontend can’t grant itself permission to access another domain.
内容的提问来源于stack exchange,提问作者Snow Bezera

