Fetch Keep Alive 未按预期工作

问题描述

我正在开发 AngularJS 1.6 版本。 在我的 app.controller.js 中,我添加了三个事件侦听器并附加了一个 deleteStudent 方法

window.addEventListener('beforeunload',this.deleteStudent,false);
    window.addEventListener(
      'unload',() => {
        this.deleteStudent
      },false
    );
    window.addEventListener('pagehide',false);

deleteStudent() {
 let Url = "http://localhost:9000/students/3";
    const deleteMethod = {
      method: 'DELETE',headers: {
        'Content-Type': 'application/json'
      },keepalive: true
    };
    fetch(Url,deleteMethod);
}

API 调用未到达后端,而是在 chrome 网络选项卡上列为待处理状态。

*上面使用的删除 API 是在 Node js 中创建的,并且可以与 postman 一起使用。

我只需要在页面卸载时调用这个 API,建议使用带有 keep-alive 属性的 fetch API true 或使用 sendbeacon( ),但我的后端 API 是 DELETE 类型,因此我不能使用 sendbeacon(),因为它只支持 POST。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)