我使用React-Django,当用户在React中从前端注册axios并向Django发送POST信号时,如果表单无效,它将显示下面的响应
xhr.js:178 POST http://localhost:8000/account/users/
signup.js:120 Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
若我在Django browsable API或Postman中使用无效字段发布相同的帖子,我会得到下面的错误,向用户显示错误是很有用的
{
"email": [
"This field may not be blank."
],
"username": [
"This field may not be blank."
]
}
如果用户在表单和表单提交后输入无效的详细信息,我如何从axios获得相同的错误消息?
请注意,如果用户在表单中键入正确的信息,axios将提供足够的详细信息。
轴心函数
axios.post('/account/users/',{data}).then(res=>console.log(res)).catch(err=>console.log(err)
很明显,这两种情况下的响应完全相同,一个状态为400(错误请求)的HTTP响应,以及您在postman中看到的身体。您的问题与Axios如何为您提供响应有关。如果您希望在Axios获得错误响应时访问响应主体,则它位于error中。回答数据
:
axios.post('/account/users/'{data})
.then(res=>console.log(res))
.catch(err=>console.log('Response body', err.response.data)