廢話不多說了,直接給大家貼代碼了。
jQuery(function ($) {// 備份jquery的ajax方法 var _ajax = $.ajax;// 重寫ajax方法,先判斷登錄在執(zhí)行success函數(shù) $.ajax = function (opt) {var _success = opt && opt.success || function (a, b) { };var _opt = $.extend(opt, {success: function (data, textStatus) {// 如果后臺將請求重定向到了登錄頁,則data里面存放的就是登錄頁的源碼,這里需要找到data是登錄頁的證據(jù)(標(biāo)記) 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);};});重寫原理為:由于閉包函數(shù)先于頁面上script加載,所以在此可以直接復(fù)寫$.ajax方法.
重點是紅色部分內(nèi)容一定要retrun 否則在使用load方法加載頁面時報錯 “Uncaught TypeError: Cannot call method 'done' of undefined。”
新聞熱點
疑難解答