How to change Content-Type to application/json React

无人久伴 提交于 2020-03-26 04:31:06

问题


I'm using axios to get contents from an api. I want to set Content-Type to application/json in React using axios. What needs to be corrected? Below is the code for reference.

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 => {

      });
  }
}

回答1:


When you do not send data (IE: no data in POST request or simple GET) axios removes content-type from headers. The solution for now seem to be to add an empty data object to the request.

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

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



来源:https://stackoverflow.com/questions/52291924/how-to-change-content-type-to-application-json-react

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!