无法使用 fetch 设置 header
can't set header with fetch
我为 post 一个 application/x-www-form-urlencoded 形式的请求设置了 header body;
这是我的代码
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
所以我无法正确接收这个请求参数。
有人遇到过吗?
CORS 只允许一部分内容类型。我为此浪费了很多时间。 :)
参见this answer。尽管 application/x-www-form-urlencoded
可能在 CORS 请求中出现,但我认为您的 header 很可能通过在您的 fetch
调用中设置 mode: 'cors'
被覆盖。
我为 post 一个 application/x-www-form-urlencoded 形式的请求设置了 header body; 这是我的代码
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
所以我无法正确接收这个请求参数。 有人遇到过吗?
CORS 只允许一部分内容类型。我为此浪费了很多时间。 :)
参见this answer。尽管 application/x-www-form-urlencoded
可能在 CORS 请求中出现,但我认为您的 header 很可能通过在您的 fetch
调用中设置 mode: 'cors'
被覆盖。