user3670534 user3670534 - 6 months ago 115
Ajax Question

Datatables 1.10.5 ajax error handler - Getting access to the http status code

I'm using Datatables 1.10.5 and I have the ajax error handler defined. I need to gain access to the actual http status code when the error fires so I can see if my user's session time has expired (HTTP 401) vs if there's something wrong on the backend such as an HTTP 500 error. Right now the techNote is always 7.

How can I get that elusive HTTP status code from the ajax transaction? I tried below, but it does not fire.

$("#example").ajaxError(function(event, jqxhr, request, settings){
alert("Failure HTTP Code:"+jqxhr.status);
});


and

$.fn.dataTable.ext.errMode = 'throw';
$('#example').on('error.dt', function(e, settings, techNote, message) {
console.log( 'An error has been reported by DataTables: ', message);
});


Does not have the information I need, or at least that I cannot find it in any of the passed variables.

Answer

Handle xhr event. When Ajax error occurs third argument json would be null and fourth argument xhr would contain jQuery XHR object. You can get the status by accessing xhr.status property.

Also see $.fn.dataTable.ext.errMode which could be used to instruct DataTables not to show the alert.

Comments