0

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 ....

1 Answer 1

2

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"}]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That was the mistake.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.