Handle ajax empty response {} table.ajax.reload();
Handle ajax empty response {} table.ajax.reload();
![latinunit](https://secure.gravatar.com/avatar/86102b7a2603f65e45dcbc07dee56914/?default=https%3A%2F%2Fvanillicon.com%2F86102b7a2603f65e45dcbc07dee56914_200.png&rating=g&size=120)
I am reloading table data using a modal query form which sends a new request to my ajax endpoint,
Problem: if my new criteria does not produce any data, I get an error in the console and the loading spinner stays spinning forever.
How do I handle an empty object on data tables? to display an alert or activate a modal i.e.
if empty response then {
$('#notificationModal').show //error message window
or alert('No Data');
{
The response is an empty object {}
var urlEndpoint = "/sch/subscriptionAPI_v59.jssp" ;
/** datatables **/
var table= $('#recipients').DataTable({
serverSide: false,
processing:true,
ajax: {
url: urlEndpoint,
data: function (d) {
let session = sessionStorage.getItem("query")
d.cr = session ? $('#created').val() : new Date().getFullYear()+"-03-01";
d.rm = $('#rm').val();
d.st = $('#superTeam').val();
d.ss = $('#status').val();
d.it = $('#invType').val();
d.oj = operatorJurisdiction;
},
type: 'POST',
headers: {},
error: function (request, status, error) {
alert(request.responseText);
}
},
This question has an accepted answers - jump to answer
Answers
Update
I think I solevd it, by sending an error response from my API if the data length is 0 but the loading spinner (processing flag) stays spinning. how do I turn it off? in this scenario
(p![](https://datatables.net/forums/uploads/editor/7o/6fmfu45cgevu.gif)
Since Datatalbes expects the ajax data to be in the
data
object by default and the row data to be in an array you should return this:Kevin
Thanks K, that seems to have done the trick.
![](https://datatables.net/forums/uploads/editor/6l/ce0wjxea4b90.png)
where in the ajax can I check for data.length on ajax response so that I can pop up and alert?
maybe something like the following
You can use the
xhr
event or theajax.dataSrc
option.Kevin
Thanks Kevin,
I found the solution with your suggestion![:) :)](https://datatables.net/forums/resources/emoji/smile.png)