数据未加载到数据表中

时间:2018-09-06 12:04:47

标签: javascript jquery ajax datatable

我已经使用以下代码使用ajax加载数据表。数据未加载到数据库中,

$('#new_table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax":{
    "url": "<?php echo base_url();?>index.php/Members/membergetajax",
    "dataType": "json",
    "type": "POST",
    "dataSrc": "",
    },
    "lengthMenu": [5, 10, 20, 50, 100],
    "pageLength": 5,
    "columnDefs": [{
        "className": "dt-center", "targets": 0
    },{
        "className": "dt-center", "targets": 1
    },{
        "className": "dt-center", "targets": 2
    }],
    "columns": [
        { "data": "id"},
        { "data": "first_name" },
        { "data": "member_id" }
    ]   
});

ajax的数据格式为

 "data":[
     {"id":"1","first_name":"Arun","member_id":"Mem01"},
     {"id":"2","first_name":"Dinesh","member_id":"  Mem02 "},
     {"id":"3","first_name":"Ramesh  ","member_id":"Mem03 "},
     {"id":"4","first_name":"Mahesh ","member_id":"Mem04 "},
     {"id":"5","first_name":"Rajesh ","member_id":"Mem05"}
 ]

请帮助我找到问题,在此先感谢

2 个答案:

答案 0 :(得分:1)

尝试添加名称属性。

"columns": [
    { "data": "id", "name":"id"},
    { "data": "first_name", "name": "first_name" },
    { "data": "member_id" , "name": "member_id" }
]   

如果您以大写形式发送参数,请尝试如下操作:{“ data”:“ id”,“ name”:“ Id”},

答案 1 :(得分:1)

根据documentation,您有两种可能性:

  • dataSrc 设置为 data ->“ dataSrc”:“数据”
  • 您的Ajax调用应仅返回数据值:

    [{
       "id": "1",
       "first_name": "Arun",
        "member_id": "Mem01"
    }, {
       "id": "2",
       "first_name": "Dinesh",
       "member_id": "  Mem02 "
    }, {
        "id": "3",
        "first_name": "Ramesh  ",
        "member_id": "Mem03 "
    }, {
        "id": "4",
        "first_name": "Mahesh ",
        "member_id": "Mem04 "
    }, {
        "id": "5",
        "first_name": "Rajesh ",
        "member_id": "Mem05"
    }]