有网络的情况下会进到ajaxError()里,会有一瞬间xhr.status===0,执行alert,然后又正常发送请求

$(document).ajaxError(function (e, xhr, options, data) {

  if (xhr.status === 403) {
    dosomething;
  } else if (xhr.status === 0){
    alert("当前网络状态不佳哦,请稍后重试");
  } else if (xhr.status === 500){
    alert("服务器挂掉了,请稍后重试");
  } else {
    alert("当前网络状态不佳哦,请稍后重试");
  }
})
阅读 2.1k
1 个回答
✓ 已被采纳新手上路,请多包涵

在xhr.status === 0 增加个ontimeout的处理

      $.ajaxSetup({ timeout: 15000 })
      $(document).ajaxError(function (e, xhr, options, data) {
      if (xhr.status === 403) {
        dosomething;
      } else if (xhr.status === 0){
        xhr.ontimeout = function () { 
          alert("当前网络状态不佳,请稍后重试");
         }
      } else if (xhr.status === 500){
        alert("服务器挂掉了,请稍后重试")
      } else {
        alert("当前网络状态不佳,请稍后重试")
      }
    })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进