I am working on a Ext-JS web application which needs to send data to the PHP server side to store. It took me a while to find out how to decodes the receiving JSON string in PHP.
Ext-JS
Assume you have a model and calling save to send a ajax request.
Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], proxy: { type: 'ajax', url : '/users' } }); var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed@sencha.com'}); user.save(); // POST to /users
PHP
If you use $_POST variable to pass the JSON string, you won’t see anything. It is just an empty array. This is because the Content-Type in the Request packet is different now, application/json. The JSON expression string in the HTTP Body part indicates it is not URL name and value pairs. In order to access that JSON string, you have to use :
file_get_contents("php://input"); // http://php.net/manual/en/wrappers.php.php
Then you can safely use json_decode to parse the JSON string into either PHP object or associative array.
thank you for this post.
Been wrestling with this one for some time.