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.
Written by Marlon Landaverde
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Filed Under
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#