ExtJS4 : read posting JSON data in PHP

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.

affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

One Response to “ExtJS4 : read posting JSON data in PHP”

  • jeff says:

    thank you for this post.

    Been wrestling with this one for some time.

  • Leave a Reply:

    Name (required):
    Mail (will not be published) (required):
    Website:
    Comment (required):
    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>