简体   繁体   English

处理表单请求时出现200状态的异常

[英]Exception when processing form request despite 200 status

I'm creating a separate Django REST-api for my ReactJS app. 我正在为我的ReactJS应用程序创建一个单独的Django REST-api。 I'm calling a fetch POST API to my endpoint to sign up users. 我正在调用一个提取POST API到我的终端来注册用户。 I'm not sure what the error means since I'm getting a status of 200. 我不确定错误意味着什么,因为我的状态为200。

My Terminal traceback: 我的终端追溯:

[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

My ReactJS code: 我的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);
  }
);

My Django view code: 我的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)

The response callback should receive returnData.data, but instead returns a TypeError in console. 响应回调应该接收returnData.data,而是在控制台中返回TypeError。

Error [Errno 54] Connection reset by peer means that loading resource (in this example, ajax response) was interrupted, but not because of server fault. 错误[Errno 54] Connection reset by peer意味着加载资源(在此示例中为ajax响应)被中断,但不是因为服务器故障。 This means that either connection was broken or client interrupted loading of that resource. 这意味着连接中断或客户端中断加载该资源。

In your case it can mean that either there is something wrong with your JavaScript code or with browser itself. 在您的情况下,它可能意味着您的JavaScript代码或浏览器本身有问题。 Check network tab in browser debugger to find that interrupted connection, that should help to trace it down. 检查浏览器调试器中的网络选项卡以查找中断的连接,这应该有助于跟踪它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ajax POST 请求 200 但表单无效 - Ajax POST request 200 but Form is not valid 获取空列表请求返回200状态代码 - get request on empty lists return 200 status code 尽管从服务器获得 200 OK,但 django 表单中的数据并未发布到 Postgressql 数据库中 - Data from django form is not being posted in Postgressql Database despite getting 200 OK from the server 在Django表单逻辑中处理GET请求 - Processing GET request in Django form logic 当请求主体很大时,nginx返回格式错误的数据包/无响应(200) - nginx returning malformed packet/no response with 200 when request body is large 在处理来自IOS设备的请求期间发生异常 - Exception happened during processing of request from IOS Device 处理来自 ('127.0.0.1', 55134) 的请求时发生异常 - Exception happened during processing of request from ('127.0.0.1', 55134) 有时$ http状态为0,但在Django上状态为200 - Sometimes $http status 0 but on django the status is 200 有多种可能时处理Django表单? - Processing a Django form when there are multiple possibilities? Django Rest Framework - 为什么在尝试使用不正确的凭据登录用户时会返回200状态代码? - Django Rest Framework - Why is a 200 status code returned when trying to login a user using incorrect credentials?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM