JS fetch,没有得到响应的标题

2022-01-20 00:00:00 fetch javascript wordpress

我正在使用 fetch 向 wordpress api 发出请求,但我在响应中得到一个空的 headers 对象,有人知道为什么吗?

I'm making a request using fetch to wordpress api, but I get a empty headers object on the response, anyone knows why?

这是我的获取操作

export const getJobs = () => {
  return dispatch => {
    fetch(endpoints.jobs)
      .then(res => res.json())
      .then(data => dispatch(setJobs(data)))
  }
};

这是我在做 res.json 之前得到的对象

and here's the object that I get before doing res.json

body: (...)
bodyUsed: false
headers: {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "url"

关于如何从响应中获取所有标头的任何想法?

any ideas on how I can get all the headers from the response?

推荐答案

使用 CORS 时访问响应头有限制.由于您的响应类型是 cors,这可能是罪魁祸首.

There is a restriction on accessing response headers when using CORS. Since your response type is cors that might be the culprit.

请参阅此 详细回答.

相关文章