require jquery.js
对$.ajax()做了基本封装,统一处理系统异常、接口异常、频繁请求接口;
若实际业务需求调用同一接口且入参一致,添加时间戳分别即可。
var G = {
tempParams:{},
ajax: function (ob,errorFun,errorSkip) {
//TODO 防重复请求
var that = this;
var interface = ob.url;
var interfaceData = typeof ob.data == 'string' ? ob.data : typeof ob.data == 'object' ? JSON.stringify(ob.data) : '';
if(that.tempParams.hasOwnProperty(interface) && that.tempParams[interface] === interfaceData) {
console.log('click so fast');
return;
} else {
that.tempParams[interface] = interfaceData;
}
ob.url = '' + ob.url;
ob.headers = {'X-Requested-With': 'XMLHttpRequest'};
if (typeof ob.error != 'function') {
ob.error = function () {
console.warn('system error.')
};
}
var tsuccessFn = ob.success;
ob.success = function (data) {
if (typeof data == "string") {
data = JSON.parse(data);
}
if(errorSkip){
return;
}
if (!data.success) { //此处看你回参格式
console.warn('error message')
if (typeof errorFun === 'function') errorFun(data);
return;
}
tsuccessFn(data);
};
var completeFn = typeof ob.complete == 'function' ? ob.complete : function () {};
ob.complete = function (data) {
completeFn(data);
delete that.tempParams[interface];
};
$.ajax(ob);
}
};