我试图使用axios进行身份验证,但是当错误为400或401时,在丢失或错误的电子邮件和密码的情况下,我无法从API获得响应消息,控制台记录axios错误只显示如下:
错误=>错误:请求失败,状态代码400位于createError (1.chunk.js:104463) at 1.chunk.js:104697 (1.chunk.js:104697) at XMLHttpRequest.handleLoad (1.chunk.js:103940)
不知道如何得到正确的响应,即:{“错误”:{“消息”:“请提供电子邮件和密码”}}。
这是我的密码
(...)
axios.post('some api for authentication',
{
email: formData.email,
password: formData.password,
remember: formData.remember
})
.then(response => {
token = response.data.session.user
console.log('response =>', token)
})
.catch((error) => {
token = null
console.log('error =>', error)
})
(...)谢谢
发布于 2020-11-20 21:25:16
对于每个axios 处理错误,底层错误数据(如message )应该在error.response.data上可用。
.catch((error) => {
token = null
console.log('error =>', error.response.data)
});您可能希望使用该示例并处理可能发生的不同类型的错误:
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});希望这能帮上忙!
https://stackoverflow.com/questions/64936523
复制相似问题