3

I'm new to Javascript and am trying to write a userscript which tracks all Ajax calls made within a specific website and if the reponse status code is not 200, I want to automatically repeat that request. Also these requests I want to repeat are all POST requests which probably makes it even harder.

I've come so far that I'm tracking all Ajax calls but since it seems to be impossible to capture the formdata sent from the POST requests, I don't know how I could repeat the failed calls.


(function() { // Overriding XMLHttpRequest
    var oldXHR = window.XMLHttpRequest;

    function newXHR() {
        var realXHR = new oldXHR();

        realXHR.addEventListener("readystatechange", function() {
            console.log("an ajax request was made")
            console.log(realXHR.status)
            //If the status is not 200, repeat the request with same data etc
        }, false);

        return realXHR;
    }

    window.XMLHttpRequest = newXHR;
})();
Viet NaM
  • 135
  • 2
  • 8
  • Have you thought about using a package such as https://github.com/jonbern/fetch-retry ? The docs also explain how to do this with exponential backoff. – Jon B Oct 02 '19 at 12:17
  • [This answer](https://stackoverflow.com/questions/11793430/retry-a-jquery-ajax-request-which-has-callbacks-attached-to-its-deferred) might help you – dganenco Oct 02 '19 at 13:19

1 Answers1

1

You can try by calling the same function again

if(realXHR.status !==200){
 newXHR();
}
brk
  • 48,835
  • 10
  • 56
  • 78