【发布时间】:2019-01-26 13:00:07
【问题描述】:
我调用了一个api方法,当响应为500服务器错误时,axios拦截器函数启动,返回Promise.reject(error);
但它停在那里,所以“then”函数永远不会运行。相反,它变成了“未捕获”:
Uncaught (in promise) Error: Request failed with status code 500
拦截器不应该将错误返回给进行api调用的函数,然后我可以在那里处理错误吗?
代码如下:
//on submitting form
onSubmit(data){
//call api function
api.sendPayment(data).then(response => {
//this never runs
alert('response!')
console.log(JSON.stringify(response))
}
});
//Api function
sendPayment: (formData) => {
return axios.post('/api/checkout', formData);
},
//interceptor
axios.interceptors.response.use(response => {
return response;
}, error => {
//this executes the "uncaught" error instead of returning to the "then" function.
return Promise.reject(error);
});
【问题讨论】: