Skip to content

Instantly share code, notes, and snippets.

@hersonls
Created October 9, 2013 18:44
Show Gist options
  • Save hersonls/6906069 to your computer and use it in GitHub Desktop.
Save hersonls/6906069 to your computer and use it in GitHub Desktop.
JavaScript: Listening all ajax events
!function ($) {
var logEvent = function(event) {
$('.notifier').append('<p>'+event+'</p>');
}
$(document).ajaxStart(onStart)
.ajaxStop(onStop)
.ajaxSend(onSend)
.ajaxComplete(onComplete)
.ajaxSuccess(onSuccess)
.ajaxError(onError);
// declare callback functions
function onStart(event) {
logEvent('Loading...');
}
function onStop(event) {
// logEvent(event);
}
function onSend(event, xhr, settings) {
logEvent('Iniciando.');
}
function onComplete(event, xhr, settings) {
logEvent('Completed.');
}
function onSuccess(event, xhr, settings) {
logEvent('Success!');
}
function onError(event, xhr, settings, err) {
// logEvent(event, xhr, settings, err);
}
$.get(
'http://127.0.0.1:8000/ajax_content.html',
function(response) {
$('.placeholder').html(response);
},
'html'
);
}(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment