1. #1
    Sencha User
    Join Date
    Sep 2010
    Location
    Netherlands
    Posts
    120
    Vote Rating
    4
    Answers
    3
    aacoro is on a distinguished road

      0  

    Default 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:

    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();
                }
            }
            }
        }
    });
    Any suggestions why the JSON cannot be read in CodeIgniter??

  2. Few changes to your model:

    Add idProperty
    Code:
        extend: 'Ext.data.Model',
        idProperty: 'id_user',
        fields: [ ..]
    or use 'id' in your fields define:
    Code:
        extend: 'Ext.data.Model',
        fields: [ { id: 'id_user', name: 'id_user', type: 'int' }, ]
    use actionMethods to change all to POST
    Code:
        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);

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

    Code:
    {"data":[
    {"id_user":1,"user_name":"ADMIN","first_name":"System","last_name":"Administrator"}]
    }
    return from CI to confirm success:
    Code:
    {"success":true,"data":
    {"id_user":"1","user_name":"ADMIN","first_name":"System","last_name":"Administrator"}
    }

    Regards,
    Scott.

  3. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,197
    Vote Rating
    482
    Answers
    690
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    Few changes to your model:

    Add idProperty
    Code:
        extend: 'Ext.data.Model',
        idProperty: 'id_user',
        fields: [ ..]
    or use 'id' in your fields define:
    Code:
        extend: 'Ext.data.Model',
        fields: [ { id: 'id_user', name: 'id_user', type: 'int' }, ]
    use actionMethods to change all to POST
    Code:
        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);

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

    Code:
    {"data":[
    {"id_user":1,"user_name":"ADMIN","first_name":"System","last_name":"Administrator"}]
    }
    return from CI to confirm success:
    Code:
    {"success":true,"data":
    {"id_user":"1","user_name":"ADMIN","first_name":"System","last_name":"Administrator"}
    }

    Regards,
    Scott.

  4. #3
    Sencha User
    Join Date
    Sep 2010
    Location
    Netherlands
    Posts
    120
    Vote Rating
    4
    Answers
    3
    aacoro is on a distinguished road

      0  

    Default


    with the following code I'm able to read the request:
    PHP Code:
    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):
    PHP Code:
    echo var_dump($_POST); 
    But no result also not with:
    PHP Code:
    echo $this->input->get_post(); 
    (or just post(), or post('tasks' ,TRUE);....)

    Thanks mate, saved my a few hours!

Thread Participants: 1

Tags for this Thread