简体   繁体   English

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

[英]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.

相关问题 在电子中使用AudioWorklet(DOMException:用户中止了请求) - Use AudioWorklet within electron (DOMException: The user aborted a request) Angular Electron 和 AudioWorklet.addModule() 错误 DOMException:用户中止请求 - Angular Electron & AudioWorklet.addModule() error DOMException: The user aborted a request 获取().then().catch(); 没有捕获`未捕获(承诺)DOMException:用户中止请求。` - fetch().then().catch(); doesn't catch `Uncaught (in promise) DOMException: The user aborted a request.` FileSystemHandle.requestPermission DOMException:需要用户激活才能请求权限 - FileSystemHandle.requestPermission DOMException: User activation is required to request permissions DOMException: 无效的图像请求 - DOMException: Invalid image request DOMException: play() 请求被中断 - DOMException: The play() request was interrupted 在JavaScript中获取“ NS_ERROR_NOT_AVAILABLE:提示被用户中止” - getting “NS_ERROR_NOT_AVAILABLE: prompt aborted by user” in javascript DOMException错误 - Javascript play()只能由用户手势启动,但我从touchStart调用它 - DOMException error - Javascript play() can only be initiated by a user gesture, but I am calling it from touchStart 使用JavaScript手动/人工抛出DOMException - Manually/Artificially throwing a DOMException with JavaScript 捕获未捕获(承诺)的DOMException javascript - catching Uncaught (in promise) DOMException javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM