首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DOMException:用户中止了一个请求(Javascript)

DOMException:用户中止了一个请求(Javascript)
EN

Stack Overflow用户
提问于 2022-09-20 08:34:43
回答 1查看 60关注 0票数 1

在我的代码中有一个timeout,所以如果在25秒内没有响应我的请求,请求就会被controller.abort()取消,并显示一个Sweetalert2通知用户一个“服务器超时”。另外,我还有另外一个一般错误的甜警报。这样用户就会知道某些东西正在失败。

问题是它没有显示超时的甜蜜警告(尽管它确实取消了请求)。相反,它显示了一般的错误甜警报。

在控制台中,我得到了错误DOMException: The user aborted a request,但无法解决它。提前谢谢。

代码:

代码语言:javascript
运行
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-20 08:40:54

此错误来自controller.abort()中止的请求,我建议您捕获错误以不停止执行。

代码语言:javascript
运行
复制
      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();
      });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73783781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档