Dva: fetch设置headers失效

Created on 15 Nov 2016  ·  11Comments  ·  Source: dvajs/dva

@sorrycc utils/request.js中设置headers,在浏览器查看http请求,发现未在request headers中生效:
request.js代码:

fetch(url, {
    body:"name=%E4%B8%AD%E6%96%87&mobile=121212121&password=111111&captcha=sdsds",
    method:"post",
    mode:"no-cors",
    headers:{
        Accept:"application/json",
        Authorization:"Bearer dfdfdfdfdfdfdf",
        Content-Type:"application/json"}
});

浏览器中Request Headers显示未生效:
accept:application/json
content-type:text/plain;charset=UTF-8

请教一下,如何设置headers头信息呢?比如将token塞进header中?

question

Most helpful comment

@xaviertung 你把 mode 设置为 'cors' 就行了,不要用 'no-cors'

All 11 comments

参考 https://www.npmjs.com/package/whatwg-fetch#post-json ,不过你的代码看起来好想也没问题。

OK,谢谢,我再尝试一下。

Content-Type 是不是该加个引号包起来.

@xaviertung 该问题你解决了吗,怎么解决的,能否请教下,我折腾了好久还是不行。

hi, @huzhengke , @lincenying 的方式应该是正解

@monkindey 加引号,按照官网的写法依然没用呀。

@huzhengke 请问解决了吗?新人又入坑了。。。。

得这样

fetch(
    new Request(`${API}${path}`, {
      headers: new Headers({ Authorization: `Bearer ${TOKEN}`, 'Content-Type': 'application/json' }),
    }),
  )
    .then(res => res.json());

关键是用 Request 和 Headers 对象。

@xaviertung 你把 mode 设置为 'cors' 就行了,不要用 'no-cors'

请问有解决方法吗

@suedar 你看我那个行不行

Was this page helpful?
0 / 5 - 0 ratings