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 }