vwvy_a
Last Updated: February 25, 2016
·
2.095K
· marlonlandaverde
Af5d5e0da79c751e743c4a634dc0bb16

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
Respond