获取响应中缺少标头 [英] Missing headers in Fetch response

查看:30
本文介绍了获取响应中缺少标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个CORS post request。我需要使用fetch,因为axiosresponse已处理为json。

但在fetch响应中,headers为空。但我不认为这是服务器问题,因为axios响应头具有我想要的值。

有什么窍门吗?

    fetch('http://localhost:9876/test/sample-download', {
        method: 'post',
        headers: {},
        body: {}
    })
        .then(response => {
            console.log(response);
        })
        .catch(err => console.error(err));

    axios.post('http://localhost:9876/test/sample-download', {}, {})
        .then(response => console.log(response))
        .catch(error => console.error(error));

推荐答案

FETCH返回的Headers类实例是iterable,而不是AXIOS返回这样的普通对象。一些可迭代的数据,如Header或URLSearchParams,不能从控制台查看,您必须迭代它并对每个元素进行console.log,如:

fetch('http://localhost:9876/test/sample-download', {
    method: 'post',
    headers: {},
    body: {}
})
.then(response => {
  // Inspect the headers in the response
  response.headers.forEach(console.log);
  // OR you can do this
  for(let entry of response.headers.entries()) {
    console.log(entry);
  }
})
.catch(err => console.error(err));

这篇关于获取响应中缺少标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆