How to show network error in DATATABLE Ajax Call

How to show network error in DATATABLE Ajax Call

SalmanSSalmanS Posts: 102Questions: 12Answers: 0
edited December 2018 in Free community support

Hi Everyone,

My server side script works fine, however when the server or network is down, i would like to show error message.

I am aware in my javascript, there is an issue with date field, where it not converting empty values, convert empty column to something meaningful but now it just showing Null.

ie., success or error

Here is my ajax call code?

`$(document).ready(function(){
            $('#empTable').DataTable({
                'processing': true,
                'serverSide': true,
                'serverMethod': 'post',
                'ajax': {
                    'url':'ajaxfile.php'
                },
                cache: false,
                'columns': [
                    { data: 'emp_no' },
                    { data: 'first_name' },
                    { data: 'last_name' },
                    { data: 'gender' },
                    { data: 'hire_date',
                    "render": function (data) {
        var dateObj = new Date(data);
        var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
        var date = ('0' + dateObj.getDate()).slice(-2);
        var year = dateObj.getFullYear();
        //var shortDate = year + '/' + month + '/' + date;
        return (date + '/' + month + '/' + year);
    } 


    },
                ],
                rowReorder: {
            dataSrc: 'id'
            }



            });
        });`

Any help is highly appreciated.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,773

    My server side script works fine, however when the server or network is down, i would like to show error message.

    You can use the jQuery error function for this. Just add it to the ajax option config. The ajax docs discuss what happens when passing the option as an object. Make sure not to use the ajax success function.

    there is an issue with date field, where it not converting empty values, convert empty column to something meaningful but now it just showing Null.

    Are you asking for debugging help with this or how to return something other than null?

    If debugging then please provide a test case replicating the issue. Otherwise in your render function you will need to check for null value and if it is then return the desired output. Otherwise return the computed date.

    Kevin

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0
    edited December 2018

    I am little confused as to how i would be using jquery error function for this? having a look at the docs its unclear.

    I tried to modify the ajax call as follows: but get no results.

                'ajax': {
                                    'url':'ajaxfile.php'
                                }.fail(function(jqXHR, textStatus, errorThrown) {
            //handle error here
        alert(xhr.status);
                        alert(thrownError);
          })
    
                      },
                 cache: false,
                                'columns': [
                                    { data: 'emp_no' },
                                    { data: 'first_name' },
                                    { data: 'last_name' },
                                    { data: 'gender' },
                                    { data: 'hire_date',
                                    "render": function (data) {
                        var dateObj = new Date(data);
                        var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
                        var date = ('0' + dateObj.getDate()).slice(-2);
                        var year = dateObj.getFullYear();
                        //var shortDate = year + '/' + month + '/' + date;
                        return (date + '/' + month + '/' + year);
                    }
    
    
                    },
                                ],
                                rowReorder: {
                            dataSrc: 'id'
                            }
    
    
    
                            });
                        });`
    
  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,773
    Answer ✓
                    }.error: function (xhr, ajaxOptions, thrownError) {
    

    There should be a comma, not a period, between the } and error.

    Here is an example with an incorrect url.
    http://live.datatables.net/zivoqozi/1/edit

    Kevin

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0
    edited December 2018

    Thanks, I done that as follows and it worked.

        'ajax': {
                            'url':'ajaxfile.php',
                            error: function(jqXHR, ajaxOptions, thrownError) {
                          alert(thrownError + "\r\n" + jqXHR.statusText + "\r\n" + jqXHR.responseText + "\r\n" + ajaxOptions.responseText);
                        }
                        },
    

    I had to figure out how to append this values in the datatable table instead of processing .... message in the window...when the network is down or any issues.

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0

    This is the best forum in the world, specially kevin and colin - they dont have to answer our queries, but they are always there, even for silly questions related or unrelated to datatables. this is the prime example of sincere and honest people in the world.

    One simple sentence

    World class free support.

This discussion has been closed.