【问题标题】:react-axios handle error don't get the message from the serverreact-axios 处理错误没有从服务器获取消息
【发布时间】:2020-08-12 02:52:29
【问题描述】:

我无法从服务器获取自定义错误消息。

我正在使用 axios 进行调用,并使用 Express 作为服务器。

我得到了错误的状态,但没有成功得到消息,只有状态。

Express-Server 响应

 res.status(420).json({ message: "some problem" });

响应 axios 调用

    export function* deleteItem(action) {
  try {
    yield put(actions.deleteItemStart());
    yield axios.delete("/items/item/" + action.itemId);

   yield put(actions.deleteItemSuccess());
  } catch (error) { 
      console.log(error);
    yield put(actions.deleteItemFailed(error));
  }
}

这是我在没有消息的情况下得到的错误

Error: Request failed with status code 420
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)

这是我的第一篇文章,希望一切都清楚。

提前感谢吉拉德 :)

【问题讨论】:

    标签: reactjs error-handling axios


    【解决方案1】:

    其实你可以用

    得到 axios 错误
    catch(e){
    
      console.log("error response:", e.response);
    }
    

    e.response 包含服务器响应(如果有)。

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 2011-10-21
      • 2016-07-19
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2018-09-21
      • 2013-02-16
      • 2018-05-15
      相关资源
      最近更新 更多