Dealing with 204 status code

Dealing with 204 status code

AlbertoGTAlbertoGT Posts: 2Questions: 1Answers: 0

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:

Here the code I have now:

table1 = $('#TraCountHeDaily1Table').DataTable({
                    "language": {
                        "loadingRecords": "No data available"
                    "ajax": {
                        "dataSrc": function (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" },
                        {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.


  • kthorngrenkthorngren Posts: 13,773Questions: 25Answers: 3,242

    Maybe you can use the xhr event to access the xhr.status and manipulate the JSON response as desired.


  • AlbertoGTAlbertoGT Posts: 2Questions: 1Answers: 0

    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:

    if(typeof json === "undefined"){
    alert('no data was found')

    How can I add this statement in Datatable?
    Many thanks,

  • allanallan Posts: 54,680Questions: 1Answers: 8,552 Site admin

    DataTables 1.11.x should actually cope okay with a 204 code.


Sign In or Register to comment.