from codeigniter query to javascript log -
i need pass query codeigniter javascript console log in-order view data in javascript have model
public function getchartdata() { $this->db->select('month,completion_percentage'); $this->db->from('monthlyreport'); $this->db->order_by('project_no', 'asc'); $query = $this->db->get(); $result = $query->result(); $data_list = array(); foreach ($result $row) { $data_list[] = $row->month; $data_list[] = $row->completion_percentage; } return $data_list; }
my controller:
public function monthlyreport() { $this->load->view('monthlyreport'); }
and internal script:
<script type="text/javascript"> $(document).ready(function){ $.ajax({ method: 'get', url: '<?php echo site_url('main/chart_api')?>', success: function (data) { console.log(data); }, error:function(data) { console.log(data); } }); }
where should go:
public function chart_api() { $data_list = $this->foo_pro->getchartdata(); echo json_encode($data_list); }
and shows console error of uncaught syntaxerror: unexpected token
you nedd add of datatype:"json" in ajax, ,
you have echo json_encode($data_list); not return json_encode($data_list); $.ajax({ method: 'get', datatype:"json", url: '<?php echo site_url('main/chart_api')?>', success: function (data) { console.log(data); }, error:function(data) { console.log(data); } });
Comments
Post a Comment