jquery ajax封装添加默认错误提示

 1 /*
 2  * 封装$.ajax函数
 3  * =============*/
 4 $.Ajax = function(url, options){
 5     if(typeof options == 'undefined'){
 6         options = url;
 7     }
 8     if(typeof url == 'string'){
 9         options.url = url;
10     }
11 
12     if(options.error == undefined){
13         options.error = ajaxErrorCallback;
14     }
15     $.ajax(options);
16 }
 1 /*
 2  * ajax全局error callback函数
 3  * =========================*/
 4 function ajaxErrorCallback(xhr, status, error){
 5     var msg;
 6     var callback = function(){};
 7     switch (xhr.status) {
 8         case 400 :
 9             msg = '服务异常';
10             break;
11         case 401 :
12             msg = '身份认证异常';
13             callback = function() {
14                 window.location.reload();
15             };
16             break;
17         case 403 :
18             msg = "权限受限";
19             break;
20         case 404 :
21             msg = "资源不存在";
22             break;
23         case 500 :
24             msg = "运行异常";
25             break;
26         default :
27             msg = "未知服务异常";
28     }
29     if (xhr.responseJSON && xhr.responseJSON.message && xhr.responseJSON.message != "") {
30         msg = xhr.responseJSON.message;
31     }
32     showDialog(msg, callback);
33 }

 

转载于:https://www.cnblogs.com/thierry/p/5603620.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值