在我的CRA项目中,我设置了axios的默认值(在根index.js
文件中)
axios.defaults.headers = {
"Content-type": "application/json",
"Accept": "application/hal+json"
};
现在,在项目的某个地方,我想发送Content-type : 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'}})
如果我注释掉缺省值,这个请求就能正常工作。另外,如果使用Content-type : multipart/form-data
,那么也会默认get覆盖。
axios版0.20.0
react版本16.13.1
这里有什么问题?
发布于 2021-04-25 10:50:46
由于默认的内容类型标头使用键Content-type
,而axios调用传递给键Content-Type
,因此头文件不会合并。将默认的内容类型标头键重命名为'Content-Type'
。
axios.defaults.headers = {
'Content-Type': 'application/json',
// ...
}
也是如此,如果使用
Content-type : multipart/form-data
,那么也会默认get覆盖。
它起作用了,因为您使用的是Content-type
,它与默认标头中使用的键相同。
https://stackoverflow.com/questions/67256771
复制相似问题