How to catch response of server-side processing if error occurs?

How to catch response of server-side processing if error occurs?

acidstoutacidstout Posts: 8Questions: 0Answers: 0
edited March 2013 in General
Hi there,

imagine the following: a user is logged in to a web application and views a table using DataTables server-side processing to fetch data. Now the user opens a second instance and logs out of the web application. So, the session of the first instance get's invalidated. If the user now tries to fetch new data (e.g. by clicking the pagination buttons) the AJAX request will fail and DataTables throws an error ("DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.").

Is there a way to catch this error, skip this message and invoke a custom handler (e.g. display a friendly message to the user that his session is invalid)?

Kind regards

Replies

  • allanallan Posts: 50,863Questions: 1Answers: 7,624 Site admin
    Currently you need to use fnServerData . In DataTables 1.10 you'll be able to do it much easier by using:

    [code]
    $('#example').dataTable( {
    ajax: {
    url: "/myUrl",
    error: function (xhr, error, thrown) {
    alert( 'You are not logged in' );
    }
    }
    } );
    [/code]

    That feature is already available in the git repo if you want to try it out, but there are about to be some heavy changes in the repo, so you might or might not want to live on the bleeding edge :-)

    Allan
  • acidstoutacidstout Posts: 8Questions: 0Answers: 0
    Cool! Thanks Allan! I'll have a look at the repository.
This discussion has been closed.