[eluser]Steve Wright[/eluser]
jQuery DataTables can be populated via ajax containing JSON object with aaData array. The script in by view is..
Code:
$(document).ready( function () {
$('#dt_test').dataTable( {
"sAjaxSource": "<?= base_url() ?>/datatables/ajaxpagelist",
"aoColumns": [
{ "mdata": "pk_pages"},
{ "mdata": "name"},
{ "mdata": "ts"}
],
"sScrollY": "200px",
"bPaginate": false
} );
} );
I'm creating the JSON response the following controller function...
Code:
function ajaxpagelist() {
$query = $this->pages_model->get_all_pages() ;
$obj = new StdClass() ;
$ojb->aaData = $query->result_array() ;
$data['json']= json_encode($ojb);
$this->load->view('json_view',$data) ;
}
and view...
Code:
<?php
$this->output->set_header('Cache-Control: no-cache, must-revalidate');
$this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Content-type: text/plain');
echo($json);
?>
The problem is that DataTables returns an error that it cannot parse JSON as it is improperly formatted. I have checked the formatting in JSONLint to confirm that the output is properly formatted. Further, if I copy/paste the JSON in a text file and serve it directly, everything works. Am I missing something in creating and sending the JSON through CI?