如何处理服务器对请求的无响应

时间:2019-08-01 11:32:46

标签: javascript html xmlhttprequest

当服务器未响应我的XMLHttpRequest()-POST时,如何处理。 可能是服务器故障或服务器未接受我的请求。我有以下代码检查服务器的响应,但是当服务器没有响应时,控制台中看不到任何错误。

if (xhr.readyState == 4 && xhr.status == "200") 
{
  if (users.result == true) 
 {
  console.log("Congrats!", " Your account is created!", "success");
  location.href = "login.html";
 }
}
else
{
  console.log(users);
}

当服务器没有响应时,响应看起来像这样-  净:: ERR_EMPTY_RESPONSE

这就是我想要的。 在下面的代码中,当单击注册按钮时,我将其禁用,并且服务器响应后,将启用该按钮。如果服务器不响应,则该按钮将永远处于禁用状态。如何解决这个问题?

$('.btn').on('click', function f1() {
 //button disabled on click
 document.getElementById("Register").disabled = true; 
 if (xhr.readyState == 4 && xhr.status == "200") 
 {
   if (users.result == true) 
  {
   console.log("Congrats!", " Your account is created!", "success");
   location.href = "login.html";
   //button enabled back when the server responds with 'ok' status
   document.getElementById("Register").disabled = false; 
  }
 }
 else
 {
   console.log(users);
   //Button enabled back when the server responds with an error
   document.getElementById("Register").disabled = false;
 }
}

0 个答案:

没有答案