Organizational Research By

Surprising Reserch Topic

handle json request in php

handle json request in php  using -'php,ajax,json,request,mime-types'

When making an ajax call, when contentType is set to application/json instead of the default x-www-form-urlencoded, server side (in PHP) can't get the post parameters.
in the following working example, if I set the contentType to "application/json" in the ajax request, PHP $_POST would be empty.  why does this happen? How can I handle a request where contentType is application/json properly in PHP?

                    cache: false,
                    type: "POST",
                    url: "xxx.php",
                    //contentType: "application/json",
                    processData: true,
                    data: {my_params:123},
                    success: function(res){

                    complete: function(XMLHttpRequest, text_status) {



asked Oct 13, 2015 by patelnikul321
0 votes

Related Hot Questions

2 Answers

0 votes

You will find unrecognised MIME types in $HTTP_RAW_POST_DATA. You can also force PHP to always populate this array (not only for unrecognised MIME types) by setting the php.ini directive always_populate_raw_post_data to true.

Raw post data will otherwise be available through the input wrapper php://input

For more information:

answered Oct 13, 2015 by vijaygupta1980
0 votes

above is technically correct, but since I don't write much PHP, this might be more helpful

xxx.php would be

$value) {
    $message = $key . ":" . $value . "\n";
    echo fwrite($file,$message);

and then you can test with

curl -X POST -H "Content-Type: application/json" -d '{"fieldA":"xyz","fieldN":"xyz"}' http://localhost/xxx.php
answered Oct 13, 2015 by param.oncemore