Paritosh Mahale Paritosh Mahale - 26 days ago 8
Javascript Question

Datatable showing :DataTables warning: table id=userdetails - Requested unknown parameter '1' for row 0, column 1

I am using datatable plugin in my project but it showing me some warning message


DataTables warning: table id=userdetails - Requested unknown parameter
'1' for row 0, column 1. For more information about this error, please
see http://datatables.net/tn/4


code is:
Html code:

<table id="userdetails">
<thead>
<tr>
<td>User Login</td>
<td>User NiceName</td>
<td>Email</td>
<td>Register Date</td>
</tr>
</thead>
<table>


ajax call

<script>
jQuery.ajax({
type : 'POST',
data : {tableName:table_name},
url: '<?php echo plugins_url() . '/pagination/ajaxResponse.php'; ?>',
success: function(data){
jQuery("#userdetails").dataTable({
data:data,
colums:[
{'data':'user_login'},
{'data': 'user_nicename'},
{'data': 'user_email'},
{'data': 'user_registered'},
]
})
}
})

</script>


server code:

$result = $wpdb->get_results("SELECT user_login,user_nicename,user_email,user_registered from ".$_POST['tableName']);
echo json_encode($result);


responce:
enter image description here
I am unable to figure out issue

Answer Source

Though you are sending data in JSON format from PHP file, you need to accept it as JSON in AJAX call.

You need to do 2 modifications:

  • add dataType:'json', to your AJAX call,
  • Change 'colums' to 'columns' as suggested by @Terry

i.e. your AJAX call should look like:

    <script>
    jQuery.ajax({
        type : 'POST',
        data : {tableName:table_name},
        dataType:'json',
        url: '<?php echo plugins_url() . '/pagination/ajaxResponse.php'; ?>',
        success: function(data){
            jQuery("#userdetails").dataTable({
                    data:data,
                    columns:[
                        {'data':'user_login'},
                        {'data': 'user_nicename'},
                        {'data': 'user_email'},
                        {'data': 'user_registered'},
                    ]
                })
        }
    })

</script>