Ajax block page request error

When sending an Ajax request, if there is no corresponding file content in the requested file, or the path is written incorrectly, the page will report an error. Here is a brief talk about the problem of avoiding page error, or the page compatibility error

xhr.onreadystatechange = function() {
  
  if(xhr.readyState == 4) {
    //验证是否有该文件,使用status来判断状态
    if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
      box.innerHTML = xhr.responseText;
    }else {
      // 错误页面,服务器错误或者未找到的错误
      console.error("错误请求")
    }
  }
}

In this way, the page will not have the error report as shown in the figure below

Errors will be output in the console

Guess you like

Origin blog.csdn.net/weixin_41040445/article/details/114889369