我正在使用Django rest-auth包和Django allauth,以允许用户使用/res-auth/注册api调用进行注册。
如果注册失败,我会将一些JSON返回给我的Javascript web应用程序,其中包含失败的原因。我很难计算出如何提取所有情况下的错误原因。以下是用户名过短时返回的JSON示例:
{
"_body":"{\"password1\":[\"This password is too short. It must contain at least 8 characters.\"]}",
"status":400,
"ok":false,
"statusText":"Bad Request",
"headers":{
"Date":[
"Sun",
" 28 Aug 2016 16:56:40 GMT"
],
"Content-Type":[
"application/json"
],
"Server":[
"WSGIServer/0.2 CPython/3.4.3"
],
"X-Frame-Options":[
"SAMEORIGIN"
],
"Access-Control-Allow-Origin":[
"*"
],
"Allow":[
"POST",
" OPTIONS"
]
},
"type":2,
"url":"http://al8f24a9.ngrok.io/rest-auth/registration/"
}
如果失败原因是针对密码还是针对用户名,我如何提取失败原因?
发布于 2020-09-25 11:52:10
我不知道是不是太晚了,但我也有同样的问题,我刚刚解决了它。如果您正在使用fetch
,那么在第一个then
之前,使用另一个then
,比如:.then(response => response.json())
。
https://stackoverflow.com/questions/39193811
复制相似问题