【问题标题】:Exception when processing form request despite 200 status尽管状态为 200,但处理表单请求时出现异常
【发布时间】:2019-06-23 15:03:52
【问题描述】:

我正在为我的 ReactJS 应用程序创建一个单独的 Django REST-api。我正在向我的端点调用 fetch POST API 来注册用户。我不确定错误的含义,因为我的状态为 200。

我的终端回溯:

[30/Jan/2019 10:09:27] "OPTIONS /newuser/ HTTP/1.1" 200 108
Exception happened during processing of request from ('127.0.0.1', 64666)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 651, in process_request_thread
self.finish_request(request, client_address)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 721, in __init__
self.handle()
  File "/Users/shiningsunnyday/Documents/GitHub/kvizo_core/web/quizkly_env/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 171, in handle
self.handle_one_request()
  File "/Users/shiningsunnyday/Documents/GitHub/kvizo_core/web/quizkly_env/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 179, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

我的 ReactJS 代码:

var csrftoken = document.getElementById('token').getAttribute('value');
    console.log(csrftoken);
fetch('http://localhost:8000/newuser/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'X-CSRFToken': csrftoken
  },
  body: JSON.stringify({
    username: this.state.username,
    password: this.state.password,
  }),
}).then(
  (response) => {
    console.log("We did it!");
    console.log(response.json);
  }
).catch(
  (error) => {
    console.log(error);
  }
);

我的 Django 查看代码:

class SignUp(APIView):

parser_classes = (JSONParser,)
permission_classes = (AllowAny,)

def post(self, request, format = None):

    print(request.data, " is request data")
    if 'username' not in request.data or 'password' not in request.data:
        raise ParseError('Username or password not provided')
    if request.user.is_authenticated:
        login(request, user)
        returnData = UserSerializer(user)
        return Response(returnData.data)

    if 'username' not in request.data or 'password' not in request.data:
        raise ParseError('Username or password not provided')

    username = request.data['username']
    password = request.data['password']
    print(username, password)

    user = User.objects.create_user(username = username, password = password)

    login(request, user)
    returnData = UserSerializer(user)
    print(returnData.data)
    return Response(returnData.data)

响应回调应该接收returnData.data,而是在控制台中返回一个TypeError。

【问题讨论】:

    标签: django reactjs forms django-rest-framework


    【解决方案1】:

    错误[Errno 54] Connection reset by peer 表示加载资源(在此示例中为 ajax 响应)被中断,但不是因为服务器故障。这意味着要么连接中断,要么客户端中断加载该资源。

    在您的情况下,这可能意味着您的 JavaScript 代码或浏览器本身有问题。检查浏览器调试器中的网络选项卡以找到中断的连接,这应该有助于追踪它。

    【讨论】:

    • 谢谢。我会在我的 API 客户端上设置一个过早的超时。
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多