0
JavaScriptのコードにリトライロジックを実装したいと思います。これは私がAPIを呼んでいる方法です:APIサーバーがhttp 500を返した場合、AJAX呼び出しは「エラー:」ブロックに入りません。
$.ajax({
url: api_url + 'report',
type: 'GET',
dataType: 'json',
async: false,
tryCount : 0,
retryLimit : 3,
headers: {
"Authorization": "Basic " + btoa(api_username + ":" + api_pass)
},
data: {start: start_date, end: end_date},
success: function(result) {
data = result.results;
console.log("success");
},
error : function(xhr, textStatus, errorThrown) {
console.log("in error");
if (textStatus == 'timeout') {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
console.log("try count:");
console.log(this.tryCount);
$.ajax(this);
return;
}
return;
}
if (xhr.status == 500) {
console.log("still 500");
} else {
console.log("still !500");
}
}
});
だから、サーバーの問題であり、それはまだ、その後に入らない上記のJSファイル内の私のコントロールHTTP 500が返されたときに「エラー:」ブロックをこの行は "console.log("エラー ");"コンソールには表示されません。
サーバが500を返す場合に、コードを再試行するロジックを正しく実装するにはどうすればいいですか?何回かx回再試行する必要がありますか?
[statusCode](http://stackoverflow.com/a/6700873/1022914)を使用してみましたか? – Mikey