Last active
June 30, 2022 20:34
-
-
Save yajra/ddb9a28dcc120d1a5d2c to your computer and use it in GitHub Desktop.
Laravel 5.x solution to redirect AJAX expired session to login page and disable DataTables error prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Requirements: | |
* - jQuery (http://jquery.com/) | |
* - DataTables (http://datatables.net/) | |
* - BootboxJS (http://bootboxjs.com/) | |
* --------------------------------------------------------------------------- | |
* Credits to https://gist.github.com/flackend/9517696 | |
* --------------------------------------------------------------------------- | |
* This monitors all AJAX calls that have an error response. If a user's | |
* session has expired, then the system will return a 401 status, | |
* "Unauthorized", which will trigger this listener and so prompt the user if | |
* they'd like to be redirected to the login page. | |
*/ | |
$(document).ajaxError(function(event, jqxhr, settings, exception) { | |
if (exception == 'Unauthorized') { | |
// Prompt user if they'd like to be redirected to the login page | |
bootbox.confirm("Your session has expired. Would you like to be redirected to the login page?", function(result) { | |
if (result) { | |
window.location = '/login'; | |
} | |
}); | |
} | |
}); | |
// disable datatables error prompt | |
$.fn.dataTable.ext.errMode = 'none'; |
"You're session".
Thank you very much for this solution. And also thank you very much for this great package for datatables, I found it very useful. Greetings from Chile.
Thank you for this, working as intended
A little late in the day, but perfect especially the datatable disable error.
Hi, thanks for this, it's what I needed.
But the error suppression will disable all errors, and not only expired session errors.
Edit: I tried like this, but errors accumulate, so the second error shows 1st and 2nd, and so on.
$.fn.dataTable.ext.errMode = 'none';
$(document).ajaxError(function (event, jqxhr, settings, exception) {
if (exception == 'Unauthorized') {
if (confirm("Session expired. Redirect?")) {
window.location = '/admin/login';
}
} else {
$('.dataTable')
.on( 'error.dt', function ( e, settings, techNote, message ) {
console.log( 'An error has been reported by DataTables: ', message + new Date());
alert(message);
} )
.DataTable();
}
});
Thank you!
I used this strategy for shutting up the Unauthorised
error and just redirect to login page but alert any other error message.
$.fn.dataTable.ext.errMode = 'none';
LaravelDataTables["dataTableBuilder"].on( 'error.dt', function ( e, settings, techNote, message ) {
settings.jqXHR.statusText === 'Unauthorized' ? window.location = '/login' : alert(message);
console.log(message);
return true;
});
@yajra salamat ulit kabayan!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. i was looking for this.