Skip to content

Instantly share code, notes, and snippets.

@calvinchoy
Last active April 4, 2020 09:00
Show Gist options
  • Save calvinchoy/5299260 to your computer and use it in GitHub Desktop.
Save calvinchoy/5299260 to your computer and use it in GitHub Desktop.
JS - ajax global callbacks #jquery
//on start
$('body').ajaxStart(function () {
$('#ajax-status').show().text("Loading...");
});
//on stop
$('body').ajaxStop(function () {
$('#ajax-status').fadeOut();
});
//on error
$('body').ajaxError(function (event, xhr, ajaxOptions, thrownError) {
if (xhr.status === 401) {
if ($("#login").is(":hidden")) {
app.showLoginForm();
}
alert("Sorry, " + xhr.responseText.toLowerCase());
// Focus cursor on field after showing alert.
$("#login").find("input[type='text']:first").focus();
}
console.log("XHR Response: " + JSON.stringify(xhr));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment