0
votes

I am implementing datatable in my project using instruction from https://datatables.net/examples/data_sources/server_side.html

Now I have implemented same jquery code like :

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php"
    } );
} ); 

but its not working in my project and gives the error of :

DataTables warning: ********** Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4

but when I specify columns in jquery like :

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php",
         "columns": [
                { "data": "id"},
                { "data": "item" },
                { "data": "name" }
         ],
    } );
} ); 

then it works perfectly, So whats the issue here, why I can not use it without specifying columns ?

Ajax Response :

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    [
      1,
      "item1",
      "name1"
    ],
    [
      2,
      "item2",
      "name2"
    ]
  ]
}
1
Can you post your json response from server processing.php? Usually this happens when the value of the corresponding key (0) is null or undefined. - Curious Mind
information added - Er.KT
If this really is the AJAX response, wrap the numbers into quotes as well., i.e "2", not 2, ... - davidkonrad
still same error - Er.KT
What´s the status? Care to accept the answer if it helped you? - Curious Mind

1 Answers

1
votes

As far as I can see, you are accessing an array of objects instead of a two dimensional array.

Datatables default behavior is accepting data in this format:

[
    [ "row 1, cell 1", "row 1, cell 2", "row 1, cell 3" ],
    [ "row 2, cell 2", "row 2, cell 2", "row 2, cell 3" ]
]

If you use a different data source format, as I believe you do, you must specify the columns so datatables can map them.

You can find more information here

If possible, provide your json response to check if this is the case.

Ok, I can see your response now, and it works if you wrap your ids in double quotes.

Try this - create a file named response.php with this content:

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    [
      "1",
      "item1",
      "name1"
    ],
    [
      "2",
      "item2",
      "name2"
    ]
  ]
}

Create another file named test.php with this content:

<head>
<script src="http://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Item</th>
            <th>Name</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>ID</th>
            <th>Item</th>
            <th>Name</th>
        </tr>
    </tfoot>
    </table>

<script>
$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "./response.php"
    } );
} );
</script>
</body>

Upload both files to your server and test it - it works.