TypeError: data is undefined when using datatable plugin

624
November 22, 2016, at 2:24 PM

I'm trying to retrieve data from the server using DataTables.I can view the json response in the network section of my browser as well.But in the console it gives me TypeError: data is undefined. Nothing shows inside the table except column names and Processing.. label.

Here is my Json response:

{"userList":[{"age":23,"userId":123,"user_name":"Mike"},{"age":22,"userId":345,"user_name":"John"},{"age":23,"userId":123,"user_name":"Peter"}]}

json response img

Java Script:

$(document).ready(function () {
       $('#mytable').dataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": "jsond",
                    "columns": [
                        {"userList": "userId"},
                        {"userList": "user_name"},
                        {"userList": "age"}
                    ]
                });
            });

Html:

<table id="mytable">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Age</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Age</th>
                    </tr>
                </tfoot>
            </table>

Can somebody suggest me a way to get rid of this error please ....

Answer 1

according to this page your data format is wrong.

Ajax sourced data

{ 
    "data":[{"age":23,"userId":123,"user_name":"Mike"},{"age":22,"userId":345,"user_name":"John"},{"age":23,"userId":123,"user_name":"Peter"}]
}
READ ALSO
JavaScript is giving different values under one variable

JavaScript is giving different values under one variable

I'm creating a loading screen that will load when a user is connecting to the game that will be constantly playing YouTube videos using the iFrame API that will be fetched from a file named callback. php.

208
Get href from link using jQuery

Get href from link using jQuery

I'm trying to print the relative URL from the href shown below in the console:.

641
How can I create an object in jquery

How can I create an object in jquery

I have created an object in jquery.

259
&ldquo;View More&rdquo; button after every three bottom scroll using jquery

“View More” button after every three bottom scroll using jquery

I have tried a lot but can't add show more button automatically just after every three scroll down. .

326