使用Javascript向服务器发送多个请求,直到服务器返回200

时间:2016-09-30 11:40:05

标签: javascript html ajax server restart

我创建了一个html页面,用户在等待另一个应用程序重新启动时会查看该页面。我创建了一个javascript脚本,它向应用程序发出多个请求,直到应用程序返回200.但是,当我尝试打开我的html页面时,浏览器只是崩溃了。任何人都可以看到我的代码有什么问题:

var responseStatus = 404;
var numberOfTries = 0;
var request = new XMLHttpRequest(); 
while(responseStatus != 200){
    console.log("Checking if restart has completed. Attempt number: " + numberOfTries);
    request.open('GET', 'applicationurl', true);
    request.onreadystatechange = function(){
        if (request.readyState === 4){
            console.log("Request Status: " + request.status);
            responseStatus = request.status;
            console.log("Response Status: " + request.status);
            if (request.status != 200) {  
                numberOfTries++;
                console.log("Restart has not yet completed! NumberOfTries: " + numberOfTries);
            }  
        }
    };
    request.send();
}
console.log("Server has successfully restarted");

3 个答案:

答案 0 :(得分:3)

这样做的最佳方式(在我看来)实际上是结合@ serge1peshcoff的两个选项。将调用编写为在响应失败时调用自身的函数,在执行此操作时设置超时。在大多数情况下,您不希望在上一个请求之后立即发送另一个请求,您希望等待至少一秒或五秒以防止向服务器发送垃圾邮件(您也没有说明您期望的客户端数量。这是什么糟糕的浏览器可能对服务器来说很糟糕,甚至可能在您的网络应用程序完全启动并返回200之前处理请求。

此外,setTimeout是比setInterval更好的方法,因为您在每次往返时都明确重置它。

借用@serge:

function tryRequest(numTries) {
  var responseStatus = 404;
  var request = new XMLHttpRequest(); 
  numTries = numTries || 0;

  console.log("Checking if restart has completed. Attempt number: " + numAttempts);
  request.open('GET', 'applicationurl', true);
  request.onreadystatechange = function(){
      numTries++

      if (request.readyState === 4){
          console.log("Request Status: " + request.status);
          responseStatus = request.status;
          console.log("Response Status: " + request.status);
          if (request.status != 200) {  
              console.log("Restart has not yet completed! NumberOfTries: " + this.attempts);
              setTimeout(tryRequest.bind(null, numTries), 5000); // five seconds
          }  
      }
  };
  request.send();

  console.log("Server has successfully restarted");
}
tryRequest();

答案 1 :(得分:1)

您的方法会发送多个请求,但不会等待他们回复。

以下是它的工作原理:

  • 创建XMLHttpRequest
  • 回调并为其分配数据
  • 此对象发送到服务器
  • 然后while循环结束并再次运行,而不是等待回调运行

结果,创建了很多XMLHttpRequest个对象,同时运行了很多请求,这不是你想要的。

你可以这两种方式:

  • 将其包装到函数中并在失败时调用它(可选);
  • 设置一些等待响应的间隔

第一种方法:

function tryRequest(numAttempts) {
  var responseStatus = 404;
  var request = new XMLHttpRequest(); 

  console.log("Checking if restart has completed. Attempt number: " + numAttempts);
  request.open('GET', 'applicationurl', true);
  request.onreadystatechange = function(){
      if (request.readyState === 4){
          console.log("Request Status: " + request.status);
          responseStatus = request.status;
          console.log("Response Status: " + request.status);
          if (request.status != 200) {  
              console.log("Restart has not yet completed! NumberOfTries: " + numAttempts);
              tryRequest(numAttempts + 1)
          }  
      }
  };
  request.send();

  console.log("Server has successfully restarted");
}
tryRequest(0);

第二种方法:

var responseStatus = 404;
var numberOfTries = 0;
var request = new XMLHttpRequest();
var interval = setInterval(tryRequest, 1000)

function tryRequest() {
  console.log("Checking if restart has completed. Attempt number: " + numberOfTries);
  request.open('GET', 'applicationurl', true);
  request.onreadystatechange = function(){
      if (request.readyState === 4){
          console.log("Request Status: " + request.status);
          responseStatus = request.status;
          console.log("Response Status: " + request.status);
          if (request.status != 200) {
              numberOfTries++;
              console.log("Restart has not yet completed! NumberOfTries: " + numberOfTries);
          } else {
              clearInterval(interval);
              console.log("Server has successfully restarted");
          }
      }
  };
  request.send();
  }

}

(免责声明:我当时没有对它们进行测试)

答案 2 :(得分:0)

您可以使用

之类的代码
var responseStatus = 404;
var numberOfTries = 0;
function check(){
        var request = new XMLHttpRequest(); 
        console.log("Checking if restart has completed. Attempt number: " + numberOfTries);
        request.open('GET', 'applicationurl', true);
        request.onreadystatechange = function(){
            if (request.readyState === 4){
               console.log("Request Status: " + request.status);
               responseStatus = request.status;
               console.log("Response Status: " + request.status);
               if (request.status != 200) {  
                  numberOfTries++;
                  console.log("Restart has not yet completed! NumberOfTries: " + numberOfTries);
                   var tmp = setTimeout(function(){ check() },5000 ); // check after 5 second
               }  elseif (request.status == 200) {  
                    console.log("Server has successfully restarted");
               }
            }
        };
        request.send();

  }

//第一次来电

check();