问题
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