Dealing with 204 status code

Dealing with 204 status code

AlbertoGTAlbertoGT Posts: 2Questions: 1Answers: 0

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

  • 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.

    Kevin

  • 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,
    Alberto.

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

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

    Allan

Sign In or Register to comment.