梧桐生矣_ 2022-07-22 11:02 采纳率: 25%
浏览 64

关闭浏览器页面怎么像后代发送请求

关闭浏览器不会向后台发请求是什么原因。谷歌,360可以,edge就不行,是用下面这个方法不行吗

mounted() {
    window.addEventListener("beforeunload", (e) => this.beforeunloadHandler(e));
    window.addEventListener("unload", (e) => this.unloadHandler(e));
  },
  destroyed() {
    window.removeEventListener("beforeunload", (e) =>
      this.beforeunloadHandler(e)
    );
    window.removeEventListener("unload", (e) => this.unloadHandler(e));
  },
  methods: {
    beforeunloadHandler() {
      this.beforeUnloadTime = new Date().getTime();
    },
    unloadHandler(e) {
      this.gapTime = new Date().getTime() - this.beforeUnloadTime;
      //判断是窗口关闭还是刷新
      if (this.gapTime <= 5) {
        var closure = document.createElement("closure");
        //src是自己的后端地址以及参数(参数看自己需求)
        closure = this.axios.put("/sysUser/quit", this.form, {
          headers: {
            "Content-Type": "application/json",
            token: window.sessionStorage.getItem("token"),
          },
        });
        //将closure加到body中时就会立刻发送请求
        document.body.appendChild(closure);
      }
    },

在主页点击浏览器的回退按钮跳转到别的页面,然后关闭浏览器,也不能发请求,是什么原因QAQ

  • 写回答

1条回答 默认 最新

  • 天問_ 2022-10-27 17:54
    关注

    进程中断了,可以使用service worker试试,它有一个独立的 worker 线程,独立于当前网页进程,有自己独立的 worker context,可以在后台发送请求

    评论

报告相同问题?

问题事件

  • 创建了问题 7月22日