Reactjs Axios拦截器-错误未返回到";然后";方法

Reactjs Axios拦截器-错误未返回到";然后";方法,reactjs,axios,Reactjs,Axios,我调用一个api方法,当响应为500服务器错误时,axios拦截器函数启动,并返回Promise.reject(错误) 但它停止在那里,因此“then”函数永远不会运行。相反,它变成了“未破坏”: 拦截器不应该将错误返回给进行api调用的函数,然后我就可以在那里处理错误了吗 以下是代码: //on submitting form onSubmit(data){ //call api function api.sendPayment(da

我调用一个api方法,当响应为500服务器错误时,axios拦截器函数启动,并返回
Promise.reject(错误)

但它停止在那里,因此“then”函数永远不会运行。相反,它变成了“未破坏”:

拦截器不应该将错误返回给进行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);
});

您需要使用
.catch
。在
中不会捕获错误。然后

api.sendPayment(data).then(response => {
    //this never runs
        alert('response!')
    console.log(JSON.stringify(response))
}).catch(err => {
    //Handle your error here
    console.log(err.response);
})

哦,是的,我没想到。谢谢
api.sendPayment(data).then(response => {
    //this never runs
        alert('response!')
    console.log(JSON.stringify(response))
}).catch(err => {
    //Handle your error here
    console.log(err.response);
})