PDA

View Full Version : Cannot read JSON in CodeIgniter



aacoro
13 Aug 2011, 5:55 AM
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:


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();
}
}
}
}
});

Any suggestions why the JSON cannot be read in CodeIgniter??:((

scottmartin
13 Aug 2011, 8:43 AM
Few changes to your model:

Add idProperty


extend: 'Ext.data.Model',
idProperty: 'id_user',
fields: [ ..]


or use 'id' in your fields define:


extend: 'Ext.data.Model',
fields: [ { id: 'id_user', name: 'id_user', type: 'int' }, ]


use actionMethods to change all to POST


proxy: {
type: 'ajax',
actionMethods: 'POST',
..
}


Also, you did not post any of your CI PHP code?

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);



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);
}


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.

Please note that I use 'data' as my root, where you use 'tasks'.

Sample json from Ext:



{"data":[
{"id_user":1,"user_name":"ADMIN","first_name":"System","last_name":"Administrator"}]
}


return from CI to confirm success:


{"success":true,"data":
{"id_user":"1","user_name":"ADMIN","first_name":"System","last_name":"Administrator"}
}



Regards,
Scott.

aacoro
13 Aug 2011, 10:40 AM
with the following code I'm able to read the request:

echo file_get_contents('php://input');

Withouth this code I still cannot read the INPUT (send by Ext JS...

My function before (was simple, just read the JSON):

echo var_dump($_POST);

But no result also not with:

echo $this->input->get_post(); (or just post(), or post('tasks' ,TRUE);....)

Thanks mate, saved my a few hours! :D