dtechlearn
dtechlearn

Reputation: 362

How to resend failed xmlHttpRequest

in my app I am trying to

  1. Handle failed xmlHttpRequest
  2. Authenticate
  3. Resend failed xmlHttpRequest

I know how to catch error specialy for failed authorization with code 401: I have special case for this type of error.

In this scenario I just want to catch it and resend.

 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;
         }
  }

I tried search online but without result.

Upvotes: 3

Views: 1611

Answers (1)

Kamal Raj
Kamal Raj

Reputation: 82

Instead of callinfg xhr.send, you should wrap all the code of your request in a function and calling it in case of error in the request. Like below

function ErrorListener() {
  makeHTTPRequest();
}

function makeHTTPRequest(){
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", ErrorListener);
oReq.open("GET", "http://www.example.org/example.txt");
oReq.send();
}

Upvotes: 1

Related Questions