这是我的想法(http://stackoverflow.com/questions/9815058/extending-jquery-ajax-success-globally):
var LOGIN_ERROR_HAS_HAPPENED = false
var INTERNAL_SERVER_ERRORS = false
$(function(){
var Original_AJAX_function = $.ajax;
$.ajax = function(options){
if(options.success){
var original_callback = options.success
}
else{
var original_callback = function(){}
}
options.success = function(data){
if(data.STATUS == "RELOGIN") {
if(!LOGIN_ERROR_HAS_HAPPENED){
LOGIN_ERROR_HAS_HAPPENED = true
Notifier.warning("Login again. Redirecting to login page")
setTimeout(function(){
window.location.href = "/login"
}, 5000)
}
return
}
original_callback(data)
}
if(options.error){
var original_failure_call_back = options.error
}
else{
var original_failure_call_back = function(){}
}
options.error = function(data){
if(!INTERNAL_SERVER_ERRORS||!LOGIN_ERROR_HAS_HAPPENED){
INTERNAL_SERVER_ERRORS = true
Notifier.warning("Oops! Something went wrong. Let us know and we will fix it" )
}
original_failure_call_back(data)
}
Original_AJAX_function(options)
}
})
我打算把这个放在 document.ready 函数中