i am working with the php project which sometimes shows error:
DataTables warning: table id=courses - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
previously it was working great as shown in images: previous image
but from yesterday it says error:images present
but this error never occurs constantly sometimes it works sometimes it shows error i am not able to identify what is the error.
**here is the ajax which i used**
var table = $('#courses').DataTable({
"processing": true,
"serverSide": true,
"ajax":{
"url": _base_url + 'admin/Course/posts',
"dataType": "json",
"type": "POST",
"contentType": "application/json; charset=utf-8",
"data": { },
// "success":function(result)
// {
// console.log(result);
// }
},
"columns": [
{ "data": "id" },
{ "data": "status" },
{ "data": "name" },
{ "data": "image" },
{ "data": "actions" },
],
"columnDefs": [ {
"targets" : '',
"orderable": false,
}
],
rowReorder: {
dataSrc: 'sort_order'
}
});
and here is the controller:
public function posts()
{
$columns = array(
0 =>'id',
1 =>'name',
);
$limit = $this->input->post('length');
$start = $this->input->post('start');
$order = $columns[$this->input->post('order')[0]['column']];
$dir = $this->input->post('order')[0]['dir'];
$modeType = "online";
$totalData = $this->Course_model->getcountcourseAndBooks('courses');
$totalFiltered = $totalData;
if(empty($this->input->post('search')['value']))
{
$posts = $this->Course_model->getLimitOrderBy('courses',$limit,$start,$order,$dir);
}
else
{
$search = $this->input->post('search')['value'];
$posts = $this->Course_model->posts_search($limit,$start,$search,$order,$dir);
$totalFiltered = $this->Course_model->posts_search_count($search);
}
$data = array();
if(!empty($posts))
{
$i= 1;
foreach ($posts as $post)
{
$nestedData['id'] = '<span class="badge badge-primary">'.$post->id.'</span>';
$i++;
$check_status = ($post->status == 1) ? 'checked' : '';
$nestedData['status'] = '<label class="switch">
<input type="checkbox" '.$check_status.' name="admin_active" data-id="'.$post->id.'" value="1" class="custom-control-input status-change" id="customSwitch2_">
<span class="slider round"></span>
</label>';
$nestedData['name'] = $post->name;
$nestedData['image'] = '<img src="'.$post->thumb_url.'" style="width:50px;">';
$nestedData['sort_order'] = '<span class="badge badge-warning">'.$post->sort_order.'</span>';
// $date =date("jS F, Y", strtotime($post->created_at));
// $nestedData['created_on'] = '<i class="fa fa-desktop"></i> '.$date.'<br><br>';
$nestedData['content_add'] = '<a href="'.base_url().'admin/chapter/content-add/'.$post->id.'" "><i class="fa fa-plus text-success fa-lg m-r-10"></i></a>';
$nestedData['chapter_add'] = '<a href="'.base_url().'admin/course/course-chapter/'.$post->id.'" "><i class="fa fa-plus text-success fa-lg m-r-10"></i></a>';
$nestedData['actions'] = '<a class="pull-right dtldata" data-link="'.base_url().'admin/course/delete/'.$post->id.'"><i class="fa fa-trash text-danger fa-lg "></i></a><a class="pull-right" href="'.base_url().'admin/course/edit/'.$post->id.'" data-id="'.$post->id.'"><i class="fa fa-edit text-info fa-lg m-r-10"></i></a>';
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($this->input->post('draw')),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
}
and here is the post error which i am getting: ajax post error
can anyone suggest me what causing the error