I am defining a global jQuery error handler for status codes 500 (retry mechanism with some message, when retry exhausted). I'd like to override any .fail()
promise that might have been added to an $.ajax
request. My code below:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function call() {
$.ajax({
url: '/rest/api/v1/some_errorneous_parth',
retryCount:3,
method: 'GET'
}).done(function(data){
console.log(data);
}).fail(function() {
console.log("should not print");
});
}
$(document).ready(function () {
$.ajaxSetup({
retryCount: 3,
error: function retry(response) {
if(response.statuscode === 500) {
if(--this.retryCount){
$.ajax(this);
}
else {
console.log("sorry, i've 'tried everything");
}
}
}
});
});
</script>
</head>
<body>
<input type="button" onclick="call()" value="submit"/>
</body>
</html>
I'd like the should not print
to not be printed if the server returns 500 until the retry count gets exhausted.