码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-06-21 15:35:28      阅读:156      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!