Dealing with 204 status code
Dealing with 204 status code
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.
Answers
Maybe you can use the
xhr
event to access thexhr.status
and manipulate the JSON response as desired.Kevin
Hi Kevin,
Thanks for the answer.
I tried "normal ajax" and then I realised that the response for an specific query I get from the external database is "undefined" object. (in his API they said this query is 204).
So the question now would be: how to deal with "undefined" object.
With normal ajax adding the code below to the successful function do the trick:
How can I add this statement in Datatable?
Many thanks,
Alberto.
DataTables 1.11.x should actually cope okay with a 204 code.
Allan
It appears there may be a case sensitivity problem on Datatables 1.11.x code file
starting at line 63598
where status is set to oSettigs.jqXhr, it should be oSettings.jqXHR, the 'hr' needs to be capitalized as well.
Thank you! Fix committed. I think we'll be doing a release of DataTables next week, which will include this.
Allan