F
F
fandorin_official2018-02-21 12:53:37
JavaScript
fandorin_official, 2018-02-21 12:53:37

How to retry a POST request after a failed request?

There is some script (Frontend).
The following lines click on the button on the site.
The button generates a POST request (XHR) https://site.net/ajax/system.php
On average, the request takes 500 ms. Sometimes it happens that this time increases to 17 seconds (for example).
And sometimes the POST request fails and we get 502().

How can the code be improved so that the execution of further code (calling the Result() function) occurs after the end of the POST request. And in case of an error (for example, 502 ()), was the button pressed again?

$('.button_htable input[name=bet]').val(parseFloat(bet).toFixed(8));
var buttonPlayBtn = $('.button_htable .clButtonPlay')[3];
buttonPlayBtn.click();
Result();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Trakhimenok, 2018-02-21
@astec

Use Observables from RxJS
https://github.com/angular/angular/issues/5876#iss...

V
Vladimir Skibin, 2018-02-21
@megafax

Catch the error event and it has a timeout status and resend the request on it

A
Ainur Valiev, 2018-02-21
@vaajnur

check xhr response code

if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
    console.log(xhr.responseText);
  }

in jquery this is success and error callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question