How to send a message back to DataTables if an error occurs server-side?
How to send a message back to DataTables if an error occurs server-side?
When responding to a DataTables AJAX request, if an error happens server-side, I want to try/catch and send back a more descriptive message of the problem, so I can better inform the user instead of just showing the default message:
DataTables warning: table id=myTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7
What's the proper way of doing this in DataTables? I have looked into the examples but it looks like all that DataTables reads is "draw", "recordsTotal", "recordsFiltered" and "data".
Answers
Hi,
You can tell DataTables what to do on error with the
$.fn.dataTable.ext.errMode
static property. There is some information about it available in theerror
documentation (the values it can take), but it really needs its own documentation page. I'll look into adding that!Allan
I had seen the error event documentation, it's triggered when the server gives an error, basically when things go out of control. I was looking for something to use when errors are caught by the server-side error handling routines.
Perhaps DataTables could use something similar to JSON-RPC 2.0 response object. It uses either
result
orerror
to indicate success/failure. In DataTables, it could be the same, or to maintain backwards compatibility, it could be either "data" or "error".When successful, the server would respond:
When unsuccessful, the response would be:
Oh - I see, sorry I misunderstood. If you send back an
error
parameter DataTables should show that.The
$.fn.dataTable.ext.errMode
could still be used with that (particularly is a non string error was returned) to do whatever display / update is needed on the client-side.Allan