DataTable().ajax.reload() with non-standard JSON response

DataTable().ajax.reload() with non-standard JSON response

fergalpowerfergalpower Posts: 1Questions: 1Answers: 0

I am trying to update my datatable with the server response.
My understanding is that DataTable().ajax.reload() needs a JSON response from my API in the format:

{"data": {"forename": "murphy"}
}

My API returns in this format:

{"status": "success",
"data": {"forename": "murphy"}
}

My ajax:

ajax: {
create: {
type: 'POST',
headers: { 'Content-Type': 'application/json' },
url: 'myurl',
data: function (d) {

                            return JSON.stringify( d.data[0] );
                        },
                        complete: function (d) {                                
                            $('.applicantsTable').DataTable().ajax.reload();

                        }
                    }
                },

I get the following error when DataTable().ajax.reload() tries to run:

Uncaught TypeError: Cannot set property 'data' of null.

There is an error in the browser: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response.

Is there any way I can just specify the 'data' part of my API response to the reload function?

Answers

  • kthorngrenkthorngren Posts: 21,146Questions: 26Answers: 4,918

    Did you follow the troubleshooting steps in the link provided in the error?
    https://datatables.net/manual/tech-notes/1

    What did you find is in the JSON response?

    Are you using -ajax.dataSrc in your Datatables init code?

    I believe for ajax.reload() the data needs to be in an array. Your response should look more like this:

    {"status": "success",
    "data": [
        {"forename": "murphy"}
      ]
    }
    

    The response data structure will also need to match the structure of your table. Without seeing what is actually returned and what your Datatables config is its hard to say what could be wrong. Please post a link to your page or a test case replicating the issue so we can offer help.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.