Ajax call with contentType: ‘application/json’ not working

When using contentType: 'application/json' you will not be able to rely on $_POST being populated. $_POST is only populated for form-encoded content types.

As such, you need to read your data from PHP raw input like this:

$input = file_get_contents('php://input');
$object = json_decode($input);

Of course if you want to send application/json you should actually send JSON, which you are not doing. You either need to build the object serialization to JSON directly, or you need to do something like this – Convert form data to JavaScript object with jQuery – to serialize the object from the form.

Honestly in your case, since you are dealing with form data, I don’t quite think the use case for using application/json is there.

Leave a Comment