Ajax Question

Why my php backend don't have data of json post?

I have got array of json:

[Object { path="/usr/share/htvcenter/storage/Windows", imgid="14698227485587"}, Object { path="/usr/share/htvcenter/storage/WindowsServer", imgid="14701636866762"}]


And I send this array with ajax:

$.ajax({
type: 'POST',
url: urlstring,
contentType: "application/json",
data: JSON.stringify(parameters),

success: function(data){
$('.lead').hide();
blackalert('Removed successfully!');
}
});


I see this in firebug for post send:
enter image description here

But server answer is with empty $_POST:
enter image description here

Server's code is:

if (isset($_GET['treeaction']) && $_GET['treeaction'] == 'remove') {
echo 'here';
var_dump($_POST); die();
}


What I am doing wrong?

Answer

When you send a POST request you don't need to set the content type to application/json. By doing so this is no longer a regular POST request (with form values), but a payload request.

To get the data from a payload request on your server you can use:

$content = file_get_contents("php://input");
var_dump(json_decode($content)); // In case the content is a json string.

If you want to send a regular request, you can use:

$.ajax({
      type: 'POST',
      ....
      data: {'data' : parameters},
      success: function(data){
        alert(data);
      }
});

Without the contentType and without the JSON.stringify.
This way you can get the data with $_POST['data']