1 year ago
#385594

Tito
how to solve DataTables warning: Invalid JSON response in codeigniter
I'm having problems displaying data into a data table, but when I check in the console the data appears and the response is 200 . but i'm having trouble displaying it in view , i get a message that the response is invalid json
this my view
<table id="tabel" class="table table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Deskripsi</th>
<th>Lokasi</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#tabel').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
"url": "<?php echo site_url('main/ajax_list')?>",
"type": "POST",
},
"columnDefs": [
{
"targets": [ 0 ],
"orderable": false,
},
],
});
});
</script>
in this my controller
public function ajax_list()
{
$list = $this->M_master->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $datas) {
$no++;
$row = array();
$row[] = $no;
$row[] = $datas->name;
$row[] = $datas->description;
$row[] = $datas->location;
$data[] = $row;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $this->M_master->count_all(),
"recordsFiltered" => $this->M_master->count_filtered(),
"data" => $data,
);
echo json_encode($output);
}
and this my model
class M_master extends CI_Model {
var $table = 'rumah';
var $column = array('name','description','location');
var $order = array('id' => 'desc');
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item)
{
if($_POST['search']['value'])
{
if($i===0)
{
$this->db->group_start();
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i)
$this->db->group_end();
}
$i++;
}
if(isset($_POST['order']))
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
public function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
public function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
}
and this response my console
where is the error? while my console managed to display the data
php
codeigniter
datatable
datatables
0 Answers
Your Answer