小编Aks*_*hay的帖子

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', this.deleteStudent, false);

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

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

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

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

onbeforeunload onunload angularjs fetch-api

3
推荐指数
1
解决办法
9506
查看次数

标签 统计

angularjs ×1

fetch-api ×1

onbeforeunload ×1

onunload ×1