JSON Ajax error when trying to load an empty table from database through MYSQLi query
JSON Ajax error when trying to load an empty table from database through MYSQLi query
Imagchine27
Posts: 7Questions: 2Answers: 1
Hi,
I'm importing data using AJAX and a MySQL queries.
Everything works fine if the table from the database has data. If not, when loading the page, it trows the default JSON error alert.
I'm not using "serverside" parameter.
How could I do to load an empty table from the database and to make datatable recognise it has no data and show the proper message?
Thanks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Are you just returning nothing when the table has no data? If so, that is the cause of the issue as an empty string isn't by itself valid JSON. You need to return your data array as normal, but just have it be empty - e.g.
{ "data": [] }
.Allan
Yes, when I return [], it don't throw error. But then the message of the table says "loading..." instead of "No data found". Curiously, when I click on a sorteable row, then It recognise the "no data found" message. I need to do this when the table is loaded or when I do a datatable ajax reload.
How could I achieve this?
If you give me a link to the page I can take a look and see why it is showing the loading message.
Allan
I am working local.
But I can paste some links to images:
Datatable:
https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table.jpg
Datatable JS (Part 1)
https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table-js-1.jpg
Datatable JS (Part 2)
https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/data-table-js-2.jpg
Datatable Ajax PHP
https://dl.dropboxusercontent.com/u/5017575/Troubleshooting/php-ajax-datatable.jpg
I pass the datatable parameters using Javascript variables.
When I click on "Añadir" (Add), delete records or edit some, the Datatable gets ajax reloaded.
I don't see where
$output
is either being defined or used in your PHP, but if DataTables is expecting data in thedata
property of the returned object, echoing[]
isn't going to help - you would need to use{ "data": [] }
as I suggested above. I would also expect that to be in anif
condition to check if it should be output or not.Allan
Yes. I used "{ "data": [] }" but the message is still "Loading...". And when I try to delete all records from the datatable (so the table from the database get cleared), it doesn clean from the datatable.
This is what happen:
I need to achieve:
* Table ajax reloaded successfully when I delete all records from the table.
* If I load the table at the beginning or after deleting: I need to show the message "No data available"
Could you use the debugger immediately after you've done the delete and the reload and I might be able to see what is going wrong that way.
Allan
Yes, actually it throws an error when I delete all rows and clean the whole table at the same time:
TypeError: c is undefined;
Description:
Ub/<()
datatables.min.js:159
ra/i()
datatables.min.js:87
ra/n.success()
datatables.min.js:87
n.Callbacks/i()
jquery.min.js:2
n.Callbacks/j.fireWith()
jquery.min.js:2
y()
jquery.min.js:4
.send/c()
And when I delete the last existing row of the table:
TypeError: f is undefined;
TypeError: c is undefined;
I have just solved the problem.
I used this code in the ajax parameters:
"dataSrc": function(data){
if(data == "no data"){
return [];
}
else {
return data.data;
}
}
Yes, if you are returning just a plain string rather than
{ "data": [] }
from above, then that is the perfect way of handling it.Allan