I try to get data from a database and display it on a datatable. This code works:
tableTest = $('#example').DataTable({
ajax: "/datatables/getSomething",
serverSide: true,
processing: true,
columns: [
{data: 'a'},
{data: 'b'},
{data: 'c'},
{data: 'd'}
],
responsive: true
});
But now I want to pass custom parameters, e.g. myCustomParams = ["foo", "bar", "top"] to the request so I can query custom data.
tableTest = $('#example').DataTable({
ajax: {
url: "/datatables/getSomething",
type: "POST",
data: myCustomParams
},
serverSide: true,
processing: true,
columns: [
{data: 'a'},
{data: 'b'},
{data: 'c'},
{data: 'd'}
],
responsive: true
});
In getSomething() I would then take the parameters (i.e. ["foo", "bar", "top"]), query the dataset I want, and then return it back.
But I would only get the error
invalid JSON response.
I looked up that error. I debugged, I looked into Developer Console-> Network-> response and saw that instead of a return value, I get the whole page back. Is there a syntax error I'm missing?
/datatables/getSomethingin your browser directly and see what it returns. It should output JSON in any case (even when there was an error). This question seems to be not related to JavaScript but to whatever language you are using on the server.