Reactjs axios未将默认内容类型头覆盖为
在我的CRA项目中,我设置了axios的默认值(在rootReactjs axios未将默认内容类型头覆盖为,reactjs,axios,create-react-app,Reactjs,Axios,Create React App,在我的CRA项目中,我设置了axios的默认值(在rootindex.js文件中) 现在我想在项目的某个地方发送内容类型:application/x-www-form-urlencoded请求,但axios仍在发出application/json请求 const data = new URLSearchParams() data.append('existingPassword', existingPassword) data.append('newPassword', newPassword)
index.js
文件中)
现在我想在项目的某个地方发送内容类型:application/x-www-form-urlencoded
请求,但axios仍在发出application/json
请求
const data = new URLSearchParams()
data.append('existingPassword', existingPassword)
data.append('newPassword', newPassword)
axios.post("http://localhost:8085/api/password-reset", data, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
如果我注释掉默认值,这个请求可以正常工作。此外,如果使用内容类型:multipart/form data
,则默认值也为get override
axios版本0.20.0
反应版本
16.13.1
这里可能会出现什么问题?由于默认内容类型标头使用键
内容类型
,并且axios调用通过键内容类型
,因此标头不会合并。将默认内容类型标题键重命名为“content-type”
axios.defaults.headers = {
'Content-Type': 'application/json',
// ...
}
此外,如果使用内容类型:multipart/form data
,则默认值也为get override
因为您使用了
内容类型
,这与默认标题中使用的键相同。捕捉得很好。Thanks@TheCoder不客气。
axios.defaults.headers = {
'Content-Type': 'application/json',
// ...
}