[英]Error: Request failed with status code 422 in javaScript
我正在使用 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
我认为您的错误是在配置 object 中使用了 2 个标头。 你可以这样写:
const config = {
headers: {
Authorization: `Bearer ${localStorage.getItem('id_token')}`,
'Content-Type': 'application/json'
}
}
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 代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.