Dojo – checking every AJAX request and error handling
I want to check every AJAX request for a “is logged out” flag. I also want to do something nice for every AJAX error, globally (in this case, I am just going to make a dialog showing the stacktrace from tomcat).
I could do something in every load: and error:, but this does it globally:
Error handling (assumes something is subscribed to the errors topic that displays a dialog, or whatever):
(function(){
var oldxhr = dojo.xhr;
dojo.xhr = function(){
return oldxhr.apply(dojo, arguments).addErrback(function(e, v){
dojo.publish("errors", [{text:e.responseText}]);
});
};
})();
Checking all JSON (assumes something has subscribed to the checklogin topic to do the logic):
dojo.contentHandlers.json = function(xhr) {
var json = dojo.fromJson(xhr.responseText || null);
dojo.publish("checklogin", [{j:json}]);
return json;
}