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?