How to handle ajax session timeout?

How to handle ajax session timeout?

raspracconraspraccon Posts: 1Questions: 1Answers: 0

Here's my code:

        $('#{{ table_id }}').DataTable({
            "columns": columns,
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "/datatable",
                "contentType": "application/json",
                "type": "POST",
                "data": function(d) {
                    d.table_format = {"id": "{{ table_id }}", "page": page};
                    d.table = "{{ table }}";
                    d.keys = {{ keys|safe }};
                    d.search_keys = {{ search_keys|default(keys)|safe }};
                    {% if search_key_types is defined %}
                    d.search_key_types = {{ search_key_types|safe }};
                    {% endif %}
                    return JSON.stringify(d);
                },
                "error": function(xhr, resp, text) {
                    if (text === 'timeout') {
                        {# do somethig#}
                    }
                 },
                 "timeout": 15000
            },

The ideal solution for me would be showing an empty table without showing the "processing" rectangle. Under normal circumstances, if an empty table is passed from the server, the library handles that and shows a perfectly fine empty table with a row saying "no data available in table". I want this to be the same, if it timed out, then show that empty table. How can I do this on the client-side?
Note that I can't change anything on the server-side.
Also, here's the timeout error I get:

Firefox can’t establish a connection to the server at [server where the table is].

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    Ideally you would have your server return JSON such as:

    {
      "data": [],
      "timeout": true
    }
    

    The data: [] is the important part for DataTables - it would result in an empty table being shown. You could use the timeout parameter to show a message to the user if you wanted.

    If you can't have the server return valid JSON then you would need to use ajax as a function to make the Ajax call yourself and translate it as required. As a function you get a callback passed in, so you could do callback({data:[]}); when you want to show no data.

    Allan

This discussion has been closed.