I would like to retry an ajax call in the case an error happens. However it keeps sending the result instead of retrying the call regardless of whether the async attribute is true or false.
Here is a sample of the code.
var retryLimit = 5;
var result = $.ajax(
url: myUrl,
method: "GET",
type: "application/json"
success: function(data){},
error: function(data){
console.log("Retry No: ", 6 - retryLimit);
retryLimit--;
if(retryLimit > 0){
$.ajax(this);
}
},
async: true, // or false does not make any difference
);
if(result.statusCode == 200 || result.statusCode == 201 ||
result.statusCode == 204){
return result;
}else{
return null;
}
So when I go to test it I purposefully make the code error out in 4 iterations and make it work on the last one. However the function has already returned null from the first iteration. How can we make it wait for all the retries in the case of a failure? Thanks.