获取Axios前端Promise以从Rails后端接收错误消息

时间:2017-12-09 02:15:50

标签: javascript ruby-on-rails error-handling promise axios

在Rails中我回来了:

render json: { errors: e.response.body.errors }, status: 400

在JavaScript中,我使用的是Axios:

axios.patch('/settings/account', {}, {params: {...
    } })
  .then((e)=> {
    ...
    console.log('submitted')
  })
  .catch(function (error, res) {
    // only get error that gives 400, but not error msg here, res isn't here
    console.log(error);
  });

如何在我的前端获得e.response.body.errors

1 个答案:

答案 0 :(得分:2)

如果服务器响应http状态代码不是2xx,则Axios拒绝承诺。我看到您正在从服务器发送错误响应,但是您是否通过http错误状态发送此响应?如果你不是,错误不是axios的错误,所以错误将显示在"然后()"钩子,因为承诺得到了解决。

如果你是,那么你应该在error.response.data

中看到错误