;(function ($) { //备份jquery的ajax方法 var _ajax = $.ajax; //重写jquery的ajax方法 $.ajax = function (opt) { //备份opt中error和success方法 var fn = { error: function (XMLHttpRequest, textStatus, errorThrown) { }, success: function (data, textStatus) { } } if (opt.error) { fn.error = opt.error; } if (opt.success) { fn.success = opt.success; } //扩展增强处理 var _opt = $.extend(opt, { error: function (XMLHttpRequest, textStatus, errorThrown) { //超时 if (textStatus == 'timeout') { $("#toastContent").html("请求超时,请稍后重新尝试"); $("#toast").show(); setTimeout(function () { $("#toast").hide(); }, 2000); } else { //错误方法增强处理 fn.error(XMLHttpRequest, textStatus, errorThrown); } }, success: function (data, textStatus) { //成功回调方法增强处理 if (!data) { $("#toastContent").html("哎呀,调用后端系统出错了"); $("#toast").show(); setTimeout(function () { $("#toast").hide(); }, 2000); } else if (!data.success && !!data.needLoginURL) { $("#toastContent").html(data.resultMsg); $("#toast").show(); setTimeout(function () { $("#toast").hide(); window.location = data.needLoginURL + "?returnURL=" + encodeURIComponent(document.location.href); }, 2000); } else if (!data.success && !data.needLoginURL) { if (!!data.resultMsg) { //由于历史问题dialog2 是指的的 一个按钮的 dialog对应的是showDialog1 if (data.displayType == "showDialog1") { $("#dialog2Content").html(data.resultMsg); $('#dialog2').show().on('click', '.weui_btn_dialog', function () { $('#dialog2').off('click').hide(); }); //由于历史问题dialog1 是指的的 一个按钮的 dialog对应的是showDialog2 } else if (data.displayType == "showDialog2") { $("#dialog1Content").html(data.resultMsg); $("#dialog1").show().on('click', '.weui_btn_dialog', function () { $("#dialog1").off('click').hide(); }); } else { $("#toastContent").html(data.resultMsg); $("#toast").show(); setTimeout(function () { $("#toast").hide(); }, 2000); } } else { $("#toastContent").html("哎呀,调用后端系统出错了"); $("#toast").show(); setTimeout(function () { $("#toast").hide(); }, 2000); } } else { fn.success(data, textStatus); } }, beforeSend: function (XHR) { //提交前回调方法 $('#loadingToast').show(); setTimeout(function () { $('#loadingToast').hide(); }, 9000); }, complete: function (XHR, TS) { //请求完成后回调函数 (请求成功或失败之后均调用)。 $('#loadingToast').hide(); } }); _ajax(_opt); }; })(jQuery);