reactjs - axios 请求,从错误对象中读取验证消息
问题描述
发送 axios put 请求并获取状态码 400 错误。
服务器发送一个用户友好的验证错误消息,它看起来像在开发工具中:
在 catch 块中,我希望收到带有 error.message 的消息,但这等于 Request failed with status code 400
try {
dispatch({
type: SETTINGS_ALERT,
})
const response = await axios.put(
`/author/me`,
{
data: {
DisplayName: displayName,
Description: description,
},
}
)
dispatch({
type: SETTINGS_UPDATE_PROFILE_SUCCESS,
payload: response.data,
})
} catch (error) {
//need to handle it here, bu error.message not works
dispatch({
type: SETTINGS_LOAD_FAIL,
error: error.message,
})
}
我怎样才能正确阅读此消息?
解决方案
您想要的详细信息在error.response.data
:
dispatch({
type: SETTINGS_LOAD_FAIL,
error: error.response.data,
})
推荐阅读
- python - 如何在不跳过的情况下在新行上打印?
- javascript - 在Javascript中的每个新字母之前拆分字符串
- python - pyInstaller 找不到数据文件
- docker-compose - WSO2 APIM 3 docker 持久化 API 数据
- css - 如何使用 HTML 和 CSS 添加垂直曲线分隔线?
- azure - 如何让 RunBook 识别 DateTime 类型?
- python - Python 库:bs4 | 需要帮助获取不同的网址
- sql-server - 从 MS Access VBA 前端在 SQL 中使用 SESSION_CONTEXT
- python - Python在冒号后用名称声明类的奇怪方式
- python - 在 for 循环外使用 for 循环变量