jquery 拦截 post 等请求实现aop效果
$(function(){
jQuery.extend({
put: function( url, data, callback, type ) {
// shift arguments if data argument was omited
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = {};
}
return jQuery.ajax({
type: "PUT",
url: url,
data: data,
success: callback,
dataType: type
});
},
del: function( url, data, callback, type ) {
// shift arguments if data argument was omited
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = {};
}
return jQuery.ajax({
type: "DELETE",
url: url,
data: data,
success: callback,
dataType: type
});
}
})
jQuery.each( [ "get", "post","put","del"], function( i, method ) {
// jQuery.get或jQuery.post为
jQuery[ method ] = function( url, data, callback, type ) {
if($("#loading")){
$("#loading").show()
}
var callbackafter = function(){
if($("#loading")){
$("#loading").hide()
}
callback.call(window,arguments[0]);
}
// 模拟重载
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
// 利用jQuery.ajax完成任务
return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callbackafter
});
};
});
})()
function disableAop(){
jQuery.each( [ "get", "post","put","del"], function( i, method ) {
// jQuery.get或jQuery.post为
jQuery[ method ] = function( url, data, callback, type ) {
// 模拟重载
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
// 利用jQuery.ajax完成任务
return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callback
});
};
});
}
拦截就是重写jquery默认的post等方法
还有一个禁用aop的就是把源码再声明一遍即可
posted on 2017-03-23 09:32 signheart 阅读(2847) 评论(0) 编辑 收藏 举报