Welcome Guest, Not a member yet? Register   Sign In
Creating JSON to be consumed by jQuery DataTables
#1

[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?


Messages In This Thread
Creating JSON to be consumed by jQuery DataTables - by El Forum - 09-07-2012, 09:42 PM
Creating JSON to be consumed by jQuery DataTables - by El Forum - 09-09-2012, 09:47 AM
Creating JSON to be consumed by jQuery DataTables - by El Forum - 09-09-2012, 11:07 AM
Creating JSON to be consumed by jQuery DataTables - by El Forum - 09-10-2012, 06:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB