【问题标题】:Firefox fetch refuses to send custom headersFirefox fetch 拒绝发送自定义标头
【发布时间】:2019-08-06 21:35:07
【问题描述】:

谷歌了一个小时,但找不到任何提示我在这里缺少什么。

This fiddle 在 Chrome 中运行良好,但 Firefox 甚至没有尝试发送 OPTIONS 请求。

fetch("http://localhost:8080/mutate?commitNow=true", {
  method: "POST",
  body: '{"set": { "name": "Alice" }}',
  //headers: {"Content-Type": "application/json"},
  headers: [ ["Content-Type", "application/json"] ]
})

firefox de 工具中没有发出网络请求,我已经确认服务器没有收到任何请求。 在 Chrome 中,同样的小提琴确实发送了预检和请求。 如果我注释掉自定义标题,它也开始在 Firefox 中正常工作。但我需要设置内容类型。

如何让 Firefox 使用 CORS fetch 发送自定义标头?

控制台中的错误信息是:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/mutate?commitNow=true. (Reason: CORS request did not succeed).

此消息没有帮助,因为甚至没有尝试过请求(见上文)

【问题讨论】:

    标签: firefox


    【解决方案1】:

    显然 Firefox 会默默地阻止从 https:// 到 http:// 的请求

    在我将代码更改为使用 fetch("https://localhost:8080 后,我看到了 OPTIONS 请求,正如预期的那样。

    【讨论】:

      【解决方案2】:

      您需要在服务器响应中添加标题“Access-Control-Allow-Headers: content-type”

      【讨论】:

        猜你喜欢
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        • 2018-01-17
        • 1970-01-01
        • 2012-11-26
        • 2014-07-07
        • 2013-08-28
        • 2015-08-29
        相关资源
        最近更新 更多