获取 Axios 前端承诺从 Rails 后端接收错误消息

Get Axios frontend Promise to receive errors msg from Rails backend

在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

如果服务器响应 http 状态代码不是 2xx,Axios 将拒绝承诺。我看到您正在从服务器发送错误响应,但是您是通过 http 错误状态发送此响应吗?如果你不是,则错误不是 axios 的错误,因此错误将显示在 "then()" 挂钩中,因为 promise 已解决。

如果是,那么您应该会看到 error.response.data

中的错误