0

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

CodingBee
  • 99
  • 3
  • 7
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – CBroe Dec 08 '21 at 07:36
  • but it happens only when this error happens otherwise it works !! if i refresh this pages 3 to 4 times it works!! no error no issues.. – CodingBee Dec 08 '21 at 07:38
  • it happens only when the error occurs if i refresh this pages 3-4 times it works no issues.. my code is working but sometimes this error apears? – CodingBee Dec 08 '21 at 07:40
  • What is the line the message is actually referring to? – CBroe Dec 08 '21 at 07:42
  • DataTables warning: table id=courses - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 – CodingBee Dec 08 '21 at 07:47
  • I meant the PHP error message ... – CBroe Dec 08 '21 at 07:58
  • What have you tried to resolve the problem? Where are you stuck? "Invalid JSON response" sounds like the PHP controller sends a malformed response - have you tried to inspect it? – Nico Haase Dec 08 '21 at 08:03

1 Answers1

0

for codeigniter projects this error was apearing because of some libraries which is not loaded in my case it was "imagick.so" so in the index file of CI project just add this line:

$_SERVER['CI_ENV'] = 'production'; //this line
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
CodingBee
  • 99
  • 3
  • 7