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

Pro*_*ox4 5 reactjs axios

我正在使用 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 => {

      });
  }
}
Run Code Online (Sandbox Code Playgroud)

Mos*_*ini 2

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

const config = {
  headers: {
    accept: 'application/json',
  },
  data: {},
};
Run Code Online (Sandbox Code Playgroud)

参考:https: //github.com/axios/axios/issues/86