【发布时间】:2020-07-28 23:57:49
【问题描述】:
当我从我的后端发送 res.status(401).send("authentication error") 并尝试在反应前端捕获它时,我无法检查错误的状态。即使错误代码是401,它总是无法通过if语句。我该如何解决这个问题,以便当状态为401时,它会通过那个if语句?
onSubmit = async (e) => {
let formData = new FormData();
formData.append('image', this.state.file);
try {
const res = await axios({
method: 'post',
url: '/api/search',
data: formData,
headers: {'Content-Type': 'multipart/form-data' }
})
} catch (err) {
// authentication required
if (err['status'] === 401) {
console.log('authentication problems');
this.props.history.push('/');
};
}
}
【问题讨论】:
标签: reactjs post axios error-code