【问题标题】:can't set header with fetch无法使用 fetch 设置标题
【发布时间】:2018-02-26 22:49:09
【问题描述】:

我设置了一个标题,用于发布带有 application/x-www-form-urlencoded 表单主体的请求; 这是我的代码

var headers = new Headers();
      headers.append('Content-Type','application/x-www-form-urlencoded');

      fetch(url, {
        method: "POST",
        mode:"cors",
        header:headers,
       body: qs.stringify(params)
    })

但是 Content-type 在请求中自动改变了

Content-Type    text/plain;charset=UTF-8

所以我无法正确接收此请求参数。 有人遇到吗?

【问题讨论】:

  • 尝试headers 而不是header,例如:fetch(url, { method: "POST", mode:"cors", headers, /...

标签: react-native fetch fetch-api


【解决方案1】:

CORS 仅允许内容类型的子集。我为此浪费了很多时间。 :)

见this answer。尽管 application/x-www-form-urlencoded 可以在 CORS 请求中使用,但我认为很可能通过在 fetch 调用中设置 mode: 'cors' 来覆盖您的标头。

【讨论】:

  • 是的。我肯定为此浪费了很多时间。切尔尼夫是对的。我也爱你,伙计!
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 2020-12-24
  • 2020-06-29
  • 2019-01-22
  • 2021-07-18
  • 1970-01-01
  • 2017-07-05
  • 2014-07-03
相关资源
最近更新 更多