我正在使用 JS axios。我不明白这个错误。 错误:
Error: Request failed with status code 422
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)
这是我的代码:
const config = {
headers: { Authorization: `Bearer ${localStorage.getItem('id_token')}`,
headers: {
// Overwrite Axios's automatically set Content-Type
'Content-Type': 'application/json'
}
}
};
const bodyParameters = {
password:values.newPassword
};
setIsLoading(true)
//localhost:5000/api/v1/user/updatepassword
Axios.post('http://127.0.0.1:5000/api/v1/user/updatepassword',
bodyParameters,
config
)
我正在 chrome 控制台中使用 fetch 进行测试。并收到此错误:
SyntaxError: Unexpected Identifier
答案 0 :(得分:0)
我认为您的错误是在配置对象中使用了 2 个标头。你可以这样写:
const config = {
headers: {
Authorization: `Bearer ${localStorage.getItem('id_token')}`,
'Content-Type': 'application/json'
}
}
答案 1 :(得分:0)
422 状态码表示您的后端理解您的请求,但无法处理您发送的正文(数据)。
你可以试试:
const config = {
method: 'post',
url: 'http://127.0.0.1:5000/api/v1/user/updatepassword',
headers: {
Authorization: `Bearer ${localStorage.getItem('id_token')}`,
'Content-Type': 'application/json'
},
body: {
password:values.newPassword
}
}
axios(config).then(response => console.log(response)).catch(err => console.error(err))
如果您遇到相同的错误,请检查您的后端是否获取了所需的所有字段。
您还可以做一件事:尝试在 Postman 上发出请求,当它起作用时,您可以直接在代码部分获取它的 axios 代码。