0

i need to pass a query from my codeigniter to javascript console log in-order to view my data in a javascript so i have my 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 as $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 it should go:

public function chart_api()
{
    $data_List = $this->foo_pro->getChartData();
    echo json_encode($data_List);
}

and it shows a console error of Uncaught SyntaxError: Unexpected token

1 Answer 1

1

You nedd to add of dataType:"JSON" in Ajax, and

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);
        } 
        });
Sign up to request clarification or add additional context in comments.

5 Comments

@Hanthony Tagam.what output are you getting from this method chart_api(); Plz paste here.
@Hanthony Tagam you have echo json_encode($data_List); not return json_encode($data_List);
i think site_url wont load my controller because it wont change in load
@HanthonyTagam if you have index.php in url you should use site_url otherwiser you can use base_url();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.