ajax分模块,模块化管理ajax(示例代码)

var ajaxDataController = function() {//打印ajax错误日志

functionprintLog(result, url, params, response) {

console.error(‘AJAX 请求异常 - %s\n错误信息:\n%c%s\n%c请求链接:%s\n%c请求参数:%c%s\n%c返回数据:%c%s‘,‘color:red;‘,

result,‘color:#333;‘,‘color:blue‘,

url,‘color:#333;‘,‘color:green‘,

JSON.stringify(params),‘color:#333;‘,‘color:#643A3A‘,

response)

}functiondataHandle(url, params, callback, async, method) {if (!method) {throw ‘method 参数未设置‘}if ((typeof params) === ‘function‘) {

callback=params

params= null}

params= params ||{};

params= $.extend({ date: newDate().getTime().toString() }, params);

async= async == null ? true: async;var ERROR_PROCESS_MODE = 0;if (typeof (params.ERROR_PROCESS_MODE) != "undefined") {if (params.ERROR_PROCESS_MODE==1) {

ERROR_PROCESS_MODE= 1;try{deleteparams.ERROR_PROCESS_MODE;

}catch(e) {

}

}

}

$.ajax({

async: async,

url: url,

dataType:‘json‘,

data: params,

type: method,

success:function(result, textStatus, xhr) {if (result.success === false) {

printLog(result, url, params, xhr.responseText)

}switch(result.status) {case 200:

callback(result);break;case 400:if(window.parent) {

alert(result.errorMessage);

}else{

alert(result.errorMessage);

}break;case 511://未登录

callback(result);break;case 512:if (ERROR_PROCESS_MODE!=1) {if(window.parent) {

alert(result.errorMessage);

}else{

alert(result.errorMessage);

}

}

callback(result);break;case 513:

callback(result);break;default://console.log(typeof xhr.responseText);

if(xhr.responseText.indexOf("")!=-1){

result={status:511,errorMessage:"登录超时,请重新登录",resultObject:null};

callback(result);break;

}if(window.parent) {

alert("系统出现异常,请稍候再试");

}else{

alert("系统出现异常,请稍候再试");

}

callback(result);break;

}

},

error:function(xhr, textStatus, error) {if(window.parent) {

alert("系统出现异常,请稍候再试");

}else{

alert("系统出现异常,请稍候再试");

}

printLog(xhr.status, textStatus+ ‘ - ‘ +error, url, params, xhr.responseText);

}

});

}return{

post:function(url, params, callback, async) {

dataHandle(url, params, callback, async,‘post‘);

},

put:function(url, params, callback, async) {

dataHandle(url, params, callback, async,‘put‘);

},

del:function(url, params, callback, async) {

dataHandle(url, params, callback, async,‘delete‘);

},

get:function(url, params, callback, async) {

dataHandle(url, params, callback, async,‘get‘);

}

};

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值