Last Updated: February 25, 2016
· marlonlandaverde

Retrieving POST data from Backbone.js in PHP

Right out-of-the-box Backbone doesn't send POST data like usual AJAX Frameworks. You can't access the information with the $_POST superglobal.

For example, if you wanna to send the following JSON string:

{"id" : 4 , "description" : "i<3Backbone"}

This isn't accessible in:

$_POST['id'] // doesn't exist
$_POST['description'] // doesn't exist

Instead, the way to access this information is to check your input stream for the raw JSON string Backbone has sent:

$var = json_decode(file_get_contents('php://input'), true); 
// returns associative array

Now, life becomes easier you:

$var['id'] == 4; //true
$var['description'] == "i<3Backbone"; //true

You can change the way Backbone sends this information by default though, either through their emulateHTTP or emulateJSON parameters.

Say Thanks