-
13 Aug 2011 5:55 AM #1
Answered: Cannot read JSON in CodeIgniter
Answered: Cannot read JSON in CodeIgniter
Hi all,
I'm desperatly trying to get Ext JS working well with CodeIgniter. I can feed my grid from CodeIgniter but after a edit I send back the data to CodeIgniter in JSON format, but then I cannot read it??
I have posted also in the CodeIgniter Forum: http://codeigniter.com/forums/viewthread/196636/
This is my store:
Any suggestions why the JSON cannot be read in CodeIgniter??Code:var taskStore = Ext.create("Ext.data.JsonStore", { autoLoad: {start: 0, limit: 25}, remoteSort: true, autoSync : false, pageSize: 10, model : Ext.define("UnplannedTask", { extend : "Ext.data.Model", fields : [ {name : 'Id', mapping : 'tp_pk'}, {name : 'Code', mapping : 'code'}, {name : 'Description', mapping : 'description'}, {name : 'Duration', mapping : 'duration'}, {name : 'Completion', type: 'date', dateFormat: 'Y-m-d h:i:s', mapping : 'requested_completion'}, {name : 'CustomerId', mapping : 'fk_customer'} ] }), proxy: { method: 'post', api : { read : baseUrl + '/index.php?r=task/ProcessTask/index', create : baseUrl + '/index.php?r=task/ProcessTask/create', update : baseUrl + '/index.php?r=task/ProcessTask/update', destroy : baseUrl + '/index.php?r=task/ProcessTask/delete' }, type: 'ajax', reader: { type: 'json', totalProperty: 'total', successProperty: 'success', root: 'tasks', idProperty: 'taskId', }, writer: { type: 'json', idProperty: 'taskId', allowSingle: false, listful : true, nameProperty: 'mapping', writeAllFields: true, root: 'tasks', listeners : { success : function (response) { console.log("try to commit..."); taskStore.commitChanges(); } } } } });
-
Best Answer Posted by scottmartin
Few changes to your model:
Add idProperty
or use 'id' in your fields define:Code:extend: 'Ext.data.Model', idProperty: 'id_user', fields: [ ..]
use actionMethods to change all to POSTCode:extend: 'Ext.data.Model', fields: [ { id: 'id_user', name: 'id_user', type: 'int' }, ]
Also, you did not post any of your CI PHP code?Code:proxy: { type: 'ajax', actionMethods: 'POST', .. }
On the PHP side: CI controller:
note: you can simply use to following just to get POST var instead of using get_input_params function if you want to only grab a variable instead of the entire json record.
this->input->get_post('variable', TRUE);
You have idProperty of taskId in you reader/writer, but your 'id' field is 'id' in your fields define? As mentioned move your idProperty up to the top of your model and remove your reader/writer and confirm the field name.Code:function get_input_params() { $result = NULL; if(function_exists('json_decode')) { $jsonData = json_decode(trim(file_get_contents('php://input')), true); $result = $jsonData['data'][0]; } return $result; } function update_user() { // also used to insert $jsonData = $this->get_input_params(); if (is_array($jsonData)) { if (isset($jsonData['id_user'])) { if ($jsonData['id_user'] > 0) { $user = $this->User_model->update_user($jsonData['id_user'], $jsonData); } else { $user = $this->User_model->insert_user($jsonData); } } $return = array( 'success' => (isset($user)) ? TRUE : FALSE, 'data' => $user ); } else { $return = array( 'success' => FALSE, 'message' => RNR_INPUT_ERROR ); } $return = json_encode($return); $this->output->set_output($return); }
Please note that I use 'data' as my root, where you use 'tasks'.
Sample json from Ext:
return from CI to confirm success:Code:{"data":[ {"id_user":1,"user_name":"ADMIN","first_name":"System","last_name":"Administrator"}] }
Code:{"success":true,"data": {"id_user":"1","user_name":"ADMIN","first_name":"System","last_name":"Administrator"} }
Regards,
Scott.
-
13 Aug 2011 8:43 AM #2Sencha - Support Team
- Join Date
- Jul 2010
- Location
- Houston, Tx
- Posts
- 7,190
- Vote Rating
- 195
- Answers
- 436
Few changes to your model:
Add idProperty
or use 'id' in your fields define:Code:extend: 'Ext.data.Model', idProperty: 'id_user', fields: [ ..]
use actionMethods to change all to POSTCode:extend: 'Ext.data.Model', fields: [ { id: 'id_user', name: 'id_user', type: 'int' }, ]
Also, you did not post any of your CI PHP code?Code:proxy: { type: 'ajax', actionMethods: 'POST', .. }
On the PHP side: CI controller:
note: you can simply use to following just to get POST var instead of using get_input_params function if you want to only grab a variable instead of the entire json record.
this->input->get_post('variable', TRUE);
You have idProperty of taskId in you reader/writer, but your 'id' field is 'id' in your fields define? As mentioned move your idProperty up to the top of your model and remove your reader/writer and confirm the field name.Code:function get_input_params() { $result = NULL; if(function_exists('json_decode')) { $jsonData = json_decode(trim(file_get_contents('php://input')), true); $result = $jsonData['data'][0]; } return $result; } function update_user() { // also used to insert $jsonData = $this->get_input_params(); if (is_array($jsonData)) { if (isset($jsonData['id_user'])) { if ($jsonData['id_user'] > 0) { $user = $this->User_model->update_user($jsonData['id_user'], $jsonData); } else { $user = $this->User_model->insert_user($jsonData); } } $return = array( 'success' => (isset($user)) ? TRUE : FALSE, 'data' => $user ); } else { $return = array( 'success' => FALSE, 'message' => RNR_INPUT_ERROR ); } $return = json_encode($return); $this->output->set_output($return); }
Please note that I use 'data' as my root, where you use 'tasks'.
Sample json from Ext:
return from CI to confirm success:Code:{"data":[ {"id_user":1,"user_name":"ADMIN","first_name":"System","last_name":"Administrator"}] }
Code:{"success":true,"data": {"id_user":"1","user_name":"ADMIN","first_name":"System","last_name":"Administrator"} }
Regards,
Scott.
-
13 Aug 2011 10:40 AM #3
with the following code I'm able to read the request:
Withouth this code I still cannot read the INPUT (send by Ext JS...PHP Code:echo file_get_contents('php://input');
My function before (was simple, just read the JSON):
But no result also not with:PHP Code:echo var_dump($_POST);
(or just post(), or post('tasks' ,TRUE);....)PHP Code:echo $this->input->get_post();
Thanks mate, saved my a few hours!


Reply With Quote