(function ($) {
/首先备份下jquery的ajax方法
var $oajax = $.ajax;
//重写jquery的ajax方法
$.ajax = function (opt) {
console.log("执行了重写Ajax");//备份opt重载方法
var fn = {
error: function(XMLHttpRequest, textStatus, errorThrown) {},
success: function(data, textStatus) {},
beforeSend: function(XHR) {},
complete: function(XHR, TS) {}
}
if (opt.error) {
fn.error = opt.error;
}
if (opt.success) {
fn.success = opt.success;
}
if (opt.beforeSend) {
fn.beforeSend = opt.beforeSend;
}
if (opt.complete) {
fn.complete = opt.complete;
}//扩展增强处理
var oopt = $.extend(opt,
{
error: function(XMLHttpRequest, textStatus, errorThrown) {
//console.log("出现错误了");
//默认处理方法
fn.error(XMLHttpRequest, textStatus, errorThrown);
},
success: function(data, textStatus) {
//console.log("执行成功了");
fn.success(data, textStatus);
},
beforeSend: function(XHR) {
//console.log("ajax之前就执行了");
fn.beforeSend(XHR);
},
complete: function(XHR, TS) {
//console.log("执行完成");
//默认处理方法
fn.complete(XHR, TS);
}
});
$oajax(oopt)
};
})(jQuery);