背景:项目前端ajax请求很多都没有写error方法,所以用户体验很不好
新来的前端负责人发现这个问题,写了一个错误处理方法,如下
1 //统一处理Ajax错误方法 2 function onAjaxError(xhr, textStatus, error) { 3 if (xhr.status == 200) { return; } 4 if (textStatus === 'timeout') { 5 alert('对不起,您访问的页面超时,请检查网络稍后重试'); 6 } 7 else { 8 if (xhr.responseText.toString().indexOf("/Authority/Login") > 0) 9 top.window.location.href = baseURL + '/Authority/Login'; 10 else { 11 alert('对不起,您访问的页面异常,请您与对应客服联系解决'); 12 } 13 } 14 }