Last Updated: February 25, 2016
·
3.181K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside