【问题标题】:Using Javascript to extract error message from JSON returned by a Django REST API call使用 Javascript 从 Django REST API 调用返回的 JSON 中提取错误消息
【发布时间】:2017-01-04 18:00:44
【问题描述】:

我将 Django rest-auth 包与 Django allauth 一起使用,以允许用户使用 /res-auth/registration 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/"
}

如果是密码或用户名,我将如何提取失败原因?

【问题讨论】:

    标签: javascript json django rest


    【解决方案1】:

    我不知道是否为时已晚,但我遇到了同样的问题,我刚刚解决了。 如果您使用的是fetch,请在第一个then 之前使用另一个then,例如:.then(response => response.json())

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多