Reactjs 检查axios库中是否返回404错误
404错误不会直接作为Reactjs 检查axios库中是否返回404错误,reactjs,api,axios,Reactjs,Api,Axios,404错误不会直接作为axios库中的res.status返回,而是作为错误抛出到catch中。 我现在的问题是,我是否可以在catch中查询错误号 .catch(error => { if(error.status === 404) // do something } 或者,我如何使用axios库来查询它是否为404错误 const handleLogin = () => { axios.post('localhost:4000', {&quo
axios
库中的res.status
返回,而是作为错误抛出到catch中。
我现在的问题是,我是否可以在catch中查询错误号
.catch(error => {
if(error.status === 404)
// do something
}
或者,我如何使用axios库来查询它是否为404错误
const handleLogin = () => {
axios.post('localhost:4000', {"email":`${email}`, "password":`${password}`})
.then(res => {
console.log(res);
console.log(res.data);
console.log(res.status);
if(res.status === 200) {
setValidationNoExist(true);
history.push({
pathname: '/app',
state: {
email: email,
name: password,
},
})
}
else if(res.status === 203) {
setValidationWrong(false);
}
else if(res.status === 404) {
setValidationPending(false);
}
else if(res.status === 204) {
setValidationNoExist(false);
}
})
.catch(error => {
console.log(error)
setValidationNoExist(false);
})
};
您能否检查
error.response.status
是否为您的catch块提供了所需的值。这是否回答了您的问题?