我在用着 React-native版本0.62.2, Axios版本^ 0.19.2 特快版^ 4.17.1
在表达即时使用
if (!user) {
return res.status(404).send({email:'email not exist'})
}
但是事情是在使用axios的前端(本机)方面,我在追赶: [错误:请求失败,状态码为404] 而不是err.response
使用axios在本机端代码
export const onLogin = userData => {
return dispatch => {
dispatch(onFetching());
Axios.post(`${Routes.login}`, userData)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
});
};
};
所以我在这里做错了吗?
这是设计使然,当默认情况下响应的状态码不在200-299范围内时,它将引发错误。
You can change this default by overwriting
validateStatus
inaxios.create
. For example to accept the default allowed status code range + 404:这样,即使状态码为404,axios也不会抛出错误。
但是不建议这样做,因为2xx以外的状态码应视为错误。要获得回应,您应该使用
.then(err => console.log(err.response)
, which in your case will be{email:'email not exist'}
Axios默认validateStatus:
Axios错误属性:错误,配置,代码,请求,响应
You can check the defaults of axios here: https://github.com/axios/axios/blob/6642ca9aa1efae47b1a9d3ce3adc98416318661c/lib/defaults.js#L79
createError: https://github.com/axios/axios/blob/master/lib/core/createError.js