No more nonsense, let me just post the code for you.
jQuery(function ($) { // 备份jquery的ajax方法 var _ajax = $.ajax; // 重写ajax方法,先判断登录在执行success函数 $.ajax = function (opt) { var _success = opt && opt.success || function (a, b) { }; var _opt = $.extend(opt, { success: function (data, textStatus) { // 如果后台将请求重定向到了登录页,则data里面存放的就是登录页的源码,这里需要找到data是登录页的证据(标记) if ((typeof data) == 'string' && data.indexOf('shangjiaAjaxExtend') != -1) { window.location.href = 'http://' + window.location.host + '/S/BusiLogin/Index'; return; } else { _success(data, textStatus); } } }); return _ajax(_opt); }; });
The rewriting principle is: since the closure function is loaded before the script on the page, the $.ajax method can be directly rewritten here.
The key point is that the content in the red part must be retrun, otherwise an error "Uncaught TypeError: Cannot call method 'done' of undefined." will be reported when using the load method to load the page.