如何重新发送失败的xmlHttpRequest

问题描述 投票:2回答:1

在我的应用程序中我想

  1. 处理失败的xmlHttpRequest
  2. 认证
  3. 重新发送失败的xmlHttpRequest

我知道如何使用代码401特别针对失败的授权捕获错误:我有这种类型的错误的特殊情况。

在这种情况下,我只想抓住它并重新发送。

 xhr.onloadend = function () {

       switch (xhr.status) {
                     .
                     .
                     .
                case 401:
                 console.log(xhr);
                 console.log("ERROR INVALID TOKEN");
                 xhr.send(); //this gives error about missing attributes
                break;
         }
  }

我尝试在线搜索,但没有结果。

javascript xmlhttprequest
1个回答
0
投票

而不是callinfg xhr.send,您应该将请求的所有代码包装在一个函数中,并在请求中出现错误时调用它。如下

function ErrorListener() {
  makeHTTPRequest();
}

function makeHTTPRequest(){
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", ErrorListener);
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();
}
© www.soinside.com 2019 - 2024. All rights reserved.