Sending JSON to PHP using ajax
Solution 1:
Lose the contentType: "application/json; charset=utf-8",
. You're not sending JSON to the server, you're sending a normal POST query (that happens to contain a JSON string).
That should make what you have work.
Thing is, you don't need to use JSON.stringify
or json_decode
here at all. Just do:
data: {myData:postData},
Then in PHP:
$obj = $_POST['myData'];
Solution 2:
That's because $_POST
is pre-populated with form data.
To get JSON data (or any raw input), use php://input
.
$json = json_decode(file_get_contents("php://input"));