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: 21,324Questions: 26Answers: 4,949

    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: 63,494Questions: 1Answers: 10,470 Site admin

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

    Allan

  • joannaromjoannarom Posts: 1Questions: 0Answers: 0

    It appears there may be a case sensitivity problem on Datatables 1.11.x code file

    starting at line 63598

    var callback = function ( json ) {       
                var status = oSettings.jqXhr
                    ? oSettings.jqXhr.status
                    : null;
    

    where status is set to oSettigs.jqXhr, it should be oSettings.jqXHR, the 'hr' needs to be capitalized as well.

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Thank you! Fix committed. I think we'll be doing a release of DataTables next week, which will include this.

    Allan

Sign In or Register to comment.