0

How to convert an object to JSON using json_encode and then send this JSON to AJAX as response?

CI_Model :

<?php

class TResponse extends CI_Model
{
    private $status;
    private $topics;

    public function __construct()
    {
        parent::__construct();
    }
}

Inside Controller :

$response = new Model\TResponse ();
$response->status = true;
echo json_encode($response);

AJAX :

$('#myform').on('submit', function (e)
        {
            e.preventDefault(); // prevent page reload
            $.ajax ({
                type : 'POST', // hide URL
                url : 'My_Controller/exec', // form validation file
                data : $('#myform').serialize (),
                dataType: 'json',
                success : function (data)
                {
                    console.log("ok");
                }
                , error: function(xhr, status, error)
                {
                    console.log(status+" "+error+" "+xhr)
                }
            });

PROBLEM :

When i execute that code result error. the error is "error Internal Server Error [Object object]". How to solve my problem?

Mr. Mike
  • 453
  • 5
  • 23
  • that won't work: `$response->status = true;` because $status is declared private. – Jeff Oct 23 '16 at 17:21
  • @jeff : i have tried to change it to public but still same error appear. other idea? – Mr. Mike Oct 23 '16 at 17:26
  • I have myself never tried to json_encode() a class object... – Jeff Oct 23 '16 at 17:34
  • @jeff : or you have any idea how to parse 1 variable and 1 array from controller to ajax in same time ? because i want to parse 1 variable and 1 array in same time i make a new object to store it. – Mr. Mike Oct 23 '16 at 17:36
  • have a look here: http://stackoverflow.com/questions/16062824/convert-object-of-class-stdclass-to-json-object – Jeff Oct 23 '16 at 17:46

2 Answers2

1

I don't understand why you're using $response = new Model\TResponse();. It not "the codeigniter way" to load a model. But I think it must be related to the problem because the code belows works perfectly for me.

Notice I have made both class properties public. Private properties are not exposed and so would not be "presented" to json_encode().

class TResponse extends CI_Model
{
    public $status = FALSE;
    public $topics = ['php', 'stackoverflow', 'json'];

    public function __construct()
    {
        parent::__construct();
    }

}

In the controller

$this->load->model('TResponse');
$this->TResponse->status = TRUE;
echo json_encode($this->TResponse);

Your javascript is fine as is.

If I use this

success: function (data) {
    console.log(data);
}

This is what the console reports

Object
  status: true
  topics: Array[3]
    0: "php"
    1: "stackoverflow"
    2: "json"
    length: 3
DFriend
  • 8,869
  • 1
  • 13
  • 26
0

Controller

    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode( $response ));

in the view

$.ajax({
            type: "POST",
            url: "<?php echo base_url('path/to_your_controller/function')?>",
            data: {some_data: val},
            success: function (data) {
                var options = '';
                $.each(data, function (key, value) {
                   // read data , each element of the JSON object
                });
             }
        });
lpz
  • 65
  • 1
  • 8