DataTables warning: table id=example - Invalid JSON response. For more information about this error

DataTables warning: table id=example - Invalid JSON response. For more information about this error

hathimhathim Posts: 2Questions: 1Answers: 0
edited June 2018 in Free community support

Hi, i am using datatables with struts 1.x with serverside processing. so i am sending the ajax request to the action servlet and processing it. I am trying to send some junk details from the server to the datatable. I am getting invalid json response. Please help me in that since I am stuck in this for more time .

Javascript code :

$(document).ready(function() {
                 $("#example").dataTable( {
                    "dom": '<"top"fl>rt<"bottom"ip><"clear">',
                     "processing": true,
                     "serverSide": true,
                     "ordering": false,
                     'bJQueryUI': true,
                     

                     "sAjaxSource": "customerPaging.do",
                     "sServerMethod": "POST",
                      columns: [
                                 { "mData": "customer_number" },
                                 { "mData": "first_name" },
                                 { "mData": "last_name" },
                                 { "mData": "no_of_purchase" }
                                  ] 
                } ); 

Json response :

{ "draw" :1,"recordsTotal": 12,"recordsFiltered": 12,"mData": [["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"],["customer_number":"2017/0/0/0/1","first_name":"namelp","last_name":"nameas","no_of_purchase":"15"]]}

Answers

  • hathimhathim Posts: 2Questions: 1Answers: 0

    The json format is a valid one

  • colincolin Posts: 15,176Questions: 1Answers: 2,589

    Hi @hathim ,

    By default, DataTables expects the data for the table to be within a data object in the Ajax response. For you, it's not, it's in mData, so you'll need to specify that with ajax.dataSrc,

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,409Questions: 26Answers: 4,789

    Also the JSON format is not valid. You can test it using jsonlint. You have an array of objects but they are enclosed in square brackets [] instead of braces {}.

    Instead of this:

    "mData": [
            ["customer_number": "2017/0/0/0/1", "first_name": "namelp", "last_name": "nameas", "no_of_purchase": "15"],
    .....
    

    It should look like this:

    "mData": [
            {"customer_number": "2017/0/0/0/1", "first_name": "namelp", "last_name": "nameas", "no_of_purchase": "15"},
    .....
    

    Kevin

This discussion has been closed.