If you were mistaken in your code in the first code, you should use this:
var_dump(json_decode(file_get_contents("php://input"))); //and not $_POST['data']
Quote from the PHP manual
php: // input is a read-only stream that allows you to read raw data from the request body.
Since in your case you are sending JSON to the body, you must read it from this stream. The usual $_POST['field_name']
method does not work because the message body is not in URLencoded format.
In the second part, you should have used this:
contentType: "application/json; charset=utf-8", url: "ajax/selectSingle.php?m=getAbsence", data: JSON.stringify({'Absence' : JSON.stringify(this)}),
UPDATE
When the request is of the application/json
content type, PHP will not parse the request and provide you with a JSON object in $_POST
, you must parse it yourself from the raw HTTP body. JSON string is retrieved using file_get_contents("php://input");
.
If you must get this using $_POST
, you must do this:
data: {"data":JSON.stringify({'Absence' : JSON.stringify(this)})},
And then in PHP do:
$json = json_decode($_POST['data']);
source share