So, I’ve been having trouble decoding valid JSON requests sent to the server from a backbone.js application. No matter what I tried, the decoded $_POST data returned null. I poked around stackoverflow and found this solution:
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
This seems like a hack so I am wondering if there is a better way to handle JSON data from backbone in PHP. What is the proper way to recieve and decode JSON data sent from a backbone.js application in PHP?
You should check for the request method used to send data to your script, like this:
The code you posted is what you should use for when REQUEST_METHOD is PUT or DELETE. It’s not a hack at all.
You can also configure Backbone to send data via POST with:
From Backbone’s manual: