error event not thrown when ready state 0

error event not thrown when ready state 0

fedeoltofedeolto Posts: 4Questions: 1Answers: 0

Hello,
I'm using datatables for server side processing and I would like following scenario to happen:

  • user changes page but he has no Internet connectivity so the ajax request remains in readystate 0 (but don't hang forever, I see that jQuery throws an error in the console immediatly)
  • I can listen to this error and show a message to the user

I saw in the docs about this event https://datatables.net/reference/event/error and works like a charm when the connection ends with an error (for example 500 from the server), but it is not raised when the request is stalled

I see that this event is fired https://datatables.net/reference/event/xhr but I would like to know if there is a better way than intercepting every single request and why error event is not raised

Answers

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    How would you do that with just plain jQuery? Have you solved that aspect elsewhere in your code?

    Allan

  • fedeoltofedeolto Posts: 4Questions: 1Answers: 0

    I use offline js in the other points where I need to detect the absence of connectivity.
    For what it concerns the reason of my question, I just saw that it's possible to use the error option inside the ajax call and it does the trick, so sorry for not seeing it before.
    I paste an example here in case someone hits this question in google

    $('#myTable').DataTable({
    serverSide: true,
    ajax: {
    url: 'http://a.sweet.url',
    error: function (jqXHR, textStatus, errorThrown) {
    aCustomErrorHandler.showMyLovelyError(jqXHR) // <--- this function invokes offline.js api and eventually detect if user lost Internet connection
    }
    },
    columns: [
    {data: 'bla', title: 'bla'},
    {data: 'bla', title: 'bla'},
    {data: 'bla', title: 'bla'},
    {data: 'bla', title: 'bla'},
    . . .
    ],
    order: [[3, 'desc']],
    . . .
    ]
    })

  • allanallan Posts: 63,457Questions: 1Answers: 10,465 Site admin

    Thanks for posting back with your solution.

    Its also worth noting that you can use ajax as a function which will completely override the DataTables default Ajax and replace it with your own.

    Allan

This discussion has been closed.