445

これはばかげているように思えるかもしれませんが、Axios でリクエストが失敗したときにエラー データを取得しようとしています。

axios
  .get('foo.com')
  .then((response) => {})
  .catch((error) => {
    console.log(error); //Logs a string: Error: Request failed with status code 404
  });

文字列の代わりに、おそらくステータス コードとコンテンツを含むオブジェクトを取得することは可能ですか? 例えば:

Object = {status: 404, reason: 'Not found', body: '404 Not found'}
4

14 に答える 14

883

表示されるのは、オブジェクトのtoStringメソッドによって返される文字列です。error(errorは文字列ではありません。)

サーバーから応答を受信した場合、errorオブジェクトには次のresponseプロパティが含まれます。

axios.get('/foo')
  .catch(function (error) {
    if (error.response) {
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    }
  });
于 2016-08-25T19:34:30.140 に答える
1

アクシオスで

    post('/stores', body).then((res) => {

        notifyInfo("Store Created Successfully")
        GetStore()
    }).catch(function (error) {

        if (error.status === 409) {
            notifyError("Duplicate Location ID, Please Add another one")
        } else {
            notifyError(error.data.detail)
        }

    })
于 2021-09-03T05:43:48.460 に答える
0

これは既知のバグです。使用してみてください "axios": "0.13.1"

https://github.com/mzabriskie/axios/issues/378

私は同じ問題を抱えていたので、結局使用し "axios": "0.12.0"ました。それは私にとってはうまくいきます。

于 2016-08-25T19:24:07.100 に答える