当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Datatables server side processing invalid Json

问题描述:

Alright so I am working on a script for datatables server side processing and I ran into an error. Whenever I load the page I get an alert saying my Json is invalid. Though I've used Jsonlin to validate it and it said my json was valid.

So here is the Json I use for testing:

 { "draw": 1, "recordsTotal": 5, "recordsFiltered": 5, "data": [{ "first_name": "Airi", "last_name": "Satou", "position": "Accountant", "office": "Tokyo" }, { "first_name": "Garrett", "last_name": "Winters", "position": "Accountant", "office": "Tokyo" }, { "first_name": "Rhona", "last_name": "Davidson", "position": "Integration Specialist", "office": "Tokyo" }, { "first_name": "Sakura", "last_name": "Yamamoto", "position": "Support Engineer", "office": "Tokyo" }] }

And this is my datatable script

 $(document).ready(function() {

var dbColumns = getColumns("<?php echo $View; ?>",'proccesTable', "<?php echo base_url(); ?>", "<?PHP echo $ViewAction; ?>");

$('#proccesTable').DataTable( {

"processing": true,

"serverSide": true,

"ajax":{

url: ' <?php echo base_url("admin/QAJAXCall/".$AJAXAction."/".$TableName); ?>',

type: "POST",

dataType: "jsonp",

data: { data : dbColumns}

},

"columns": [

{ "data": "first_name" },

{ "data": "last_name" },

{ "data": "position" },

{ "data": "office" }

]

});

});

In the post I just try to do something else which isn't doing anything with my json being returned. Though I've noticed, there is always a NULL at the end of my Json? This is how I return the json to my datatable:

 $array = '{

"draw": 1,

"recordsTotal": 5,

"recordsFiltered": 5,

"data": [{

"first_name": "Airi",

"last_name": "Satou",

"position": "Accountant",

"office": "Tokyo"

}, {

"first_name": "Garrett",

"last_name": "Winters",

"position": "Accountant",

"office": "Tokyo"

}, {

"first_name": "Rhona",

"last_name": "Davidson",

"position": "Integration Specialist",

"office": "Tokyo"

}, {

"first_name": "Sakura",

"last_name": "Yamamoto",

"position": "Support Engineer",

"office": "Tokyo"

}]

}';

echo $array;

I dont use echo json_encode($array) since the variable is json already. Is there any reason that this "NULL" is being sent back aswell??

网友答案:

you can also use datatable library for codeigniter only in order to make correct JSON response. so, it would be easy for you rather than creating response manually.

  1. https://github.com/IgnitedDatatables/Ignited-Datatables
  2. https://github.com/zepernick/Codeigniter-DataTables

Thanks

分享给朋友:
您可能感兴趣的文章:
随机阅读: