【问题标题】:Preflight request failing on calling a POST request with keepalive using fetch api使用 fetch api 调用带有 keepalive 的 POST 请求时,预检请求失败
【发布时间】:2019-09-08 07:15:23
【问题描述】:

我在使用启用了keepalive 的获取API 调用POST 请求时收到错误Preflight request for request with keepalive specified is currently not supported。任何帮助,将不胜感激 。我在 beforeunload 事件上调用这个 api。

API 请求

  fetch(uri, {
    method: 'POST',
    headers: {
      'Content-type': options.headers.get('content-type'),
      'Authorization': options.headers.get('authorization')
    },
    body: JSON.stringify(interactionBody),
    keepalive: true
  }).catch((e) => {
    console.log(e);
  });

【问题讨论】:

标签: api fetch-api preflight


【解决方案1】:

the Chrome issue fixed之前我找到了解决方法

使用mode: 'same-origin' 时可以正常工作

fetch(uri, {
  method: 'POST',
  headers: {
    'Content-type': options.headers.get('content-type'),
    'Authorization': options.headers.get('authorization')
  },
  body: JSON.stringify(interactionBody),
  mode: 'same-origin',
  keepalive: true
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2014-07-20
    • 2018-07-04
    相关资源
    最近更新 更多