[英]DOMException: The user aborted a request (Javascript)
In my code there is a timeout
so that if in 25 seconds there is no response to my request, the request is cancelled with controller.abort()
and shows a Sweetalert2 informing the users a "server timeout".在我的代码中有一个
timeout
,因此如果在 25 秒内我的请求没有响应,则使用controller.abort()
取消请求,并显示一个 Sweetalert2 通知用户“服务器超时”。 And also I have another Sweetalert for errors in general.而且我还有另一个 Sweetalert 一般错误。 So that the users know that something is failing.
以便用户知道某些事情失败了。
The problem is that it does NOT show the Sweetalert of the timeout (although it does cancel the request).问题是它没有显示超时的 Sweetalert (尽管它确实取消了请求)。 And instead it shows the general error Sweetalert.
相反,它显示一般错误 Sweetalert。
In the console I get the error DOMException: The user aborted a request
but I can't solve it.在控制台中,我收到错误
DOMException: The user aborted a request
but I can't solve it。 Thanks in advance.提前致谢。
Code:代码:
const envSoli = async () => {
try {
const controller = new AbortController();
const signal = controller.signal;
const timeId = setTimeout(() => {
Swal.fire({
//error timeout
//custom function translate
text: translate("text1"),
icon: "info",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-info",
},
//refresh on ok click button
}).then(function () {
location.reload();
});
//controller.abort(), the idea is abort a request
//after show the sweetalert
controller.abort();
}, 20 * 1000); // 20 sec
let peticion = await fetch("data.php", {
method: "POST",
body: "ajax=1&do=check&lista=" + encodeURIComponent(leray[chenille]),
headers: { "Content-type": "application/x-www-form-urlencoded" },
cache: "no-cache",
signal: signal,
});
clearTimeout(timeId);
let oreen = await peticion.json();
switch (oreen.enviando) {
case -1:
chenille++;
document.getElementById("div1").append(oreen.cat + "<br />");
updateProgress(chenille, leray.length);
tvmit_wrongUp();
break;
case 1:
chenille++;
document.getElementById("div1").append(oreen.dog + "<br />");
updateProgress(chenille, leray.length);
tvmit_wrongUp();
break;
case 2:
chenille++;
document.getElementById("div2").append(oreen.sky + "<br />");
nieva++;
updateProgress(chenille, leray.length);
tvmit_dieUp();
break;
case 3:
chenille++;
document.getElementById("div3").append(oreen.water + "<br />");
tvmit_liveUp();
updateProgress(chenille, leray.length);
break;
}
OKTY(leray, chenille, aarsh, nieva);
return true;
} catch (error) {
console.log(error);
Swal.fire({
text: translate("text2"),
icon: "question",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-primary",
},
//refresh again on button click
}).then(function () {
location.reload();
});
}
};
envSoli();
This error came from the aborted request by controller.abort()
, I suggest you catch the error to not stop execution.此错误来自
controller.abort()
中止的请求,我建议您捕获错误以不停止执行。
try {
controller.abort();
} catch (e) {
console.log(e)
}
Swal.fire({
//error timeout
//custom function translate, to auto translate the text
text: translate("text1"),
icon: "info",
buttonsStyling: false,
confirmButtonText: translate("confirmbtn"),
allowOutsideClick: false,
allowEscapeKey: false,
customClass: {
confirmButton: "btn btn-info",
},
//refresh on ok click button
}).then(function () {
location.reload();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.