如何将 Content-Type 更改为 application/json React

2024-05-08

我正在使用 axios 从 api 获取内容。我想使用 axios 在 React 中将 Content-Type 设置为 application/json 。需要纠正什么? 下面是参考代码。

const config = {
  headers: {
    'Content-Type': 'application/json',
    'accept':'application/json'
  },
};

export function getDetails(){
    return function (dispatch) {
    return axios.get('url',config)
      .then(response => {
        if (response.status === 200) {
          console.log("actions ",response.data);
        }
      }).catch(err => {

      });
  }
}

当您不发送数据时(即:POST 请求或简单的 GET 中没有数据),axios 将从标头中删除内容类型。 目前的解决方案似乎是向请求添加一个空数据对象。

const config = {
  headers: {
    accept: 'application/json',
  },
  data: {},
};

Ref: https://github.com/axios/axios/issues/86 https://github.com/axios/axios/issues/86

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Content-Type 更改为 application/json React 的相关文章

随机推荐