首页 > Web开发 > 详细

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

时间:2016-06-21 15:34:19      阅读:237      评论:0      收藏:0      [点我收藏+]
 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 }

 

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

原文:http://www.cnblogs.com/thierry/p/5603620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!