Hi,
I am getting data from an external database using ajax and works fine except when I get 204 code status. I would like to implement an if statement which shows an alert if the status code is 204 and show the data otherwise. I tried what has been suggested below, but I could not find a solution:
- https://datatables.net/forums/discussion/48805/ajax-204-nocontent-with-no-json-in-the-response-will-fire-the-ajax-error-handler
- https://datatables.net/forums/discussion/comment/117027/#Comment_117027
Here the code I have now:
table1 = $('#TraCountHeDaily1Table').DataTable({
"language": {
"loadingRecords": "No data available"
},
"ajax": {
"type":"GET",
"dataType":'json',
"url":'https://webtris.highwaysengland.co.uk/api/v1.0/reports/Daily?sites='+siteid+'&start_date='+dateFromDef+'&end_date='+dateToDef+'&page=1&page_size=40000&reportSubTypeId=0',
"dataSrc": function (json) {
console.log(json);
return json.Rows;
}
},
"columns": [
{ "data": "Report Date" },
{ "data": "Time Period Ending" },
{ "data": "Time Interval" },
{ "data": "0 - 520 cm" },
{ "data": "521 - 660 cm" },
{ "data": "661 - 1160 cm" },
],
columnDefs:[
{targets:0, "type": "date", "render": function(data, type) {
return type === 'sort' ? data : moment(data).format('L');
}
}
]
});
I am want to implement something like the code below, but it does not work:
"dataSrc": function (json) {
if(json == {}) { //empty json response
alert('No data has been found')
} else {
return json.Rows;
}
}
Many thanks.
Alberto.