0
私はPromise内でXMLHttpRequest
を使用します。サーバーがアイドル状態になることがあるため、エラーが発生した場合は3つのアテンプトを実行したいと考えています。XMLHttpRequest - xhrエラーのデータを再送信
ただし、以下のようにObject state must be opened
行のエラーxhr.send()
をfunction sendData()
にしてください。どうして?
私はxhrが既に開いていると思います。これを達成するための正しい方法は何でしょうか?
function _callService(url, postData) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
var attempts = 0;
xhr.open("POST", url);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
resolve(xhr.response);
}
}
};
xhr.addEventListener("error", onXhrError);
function sendData() {
//here I get the Object state must be opened when this is called from onXhrError listener
xhr.send(postData);
};
function onXhrError() {
console.log("onXhrError");
if (attempts < 3) {
attempts += 1;
sendData();
} else {
reject("OnXhrError")
}
};
sendData();
});
エラーが発生した場合XHR状態が常に行われているのですか? –
参照_完了\t完了(数値4)\t転送中にデータ転送が完了したか、転送中に問題が発生しました(無限リダイレクトなど)。 "_ https://xhr.spec.whatwg.org/#states、http ://jsfiddle.net/uxae61c8/ – guest271314
fantastic!あなたの助けをたくさんありがとう! –