[英]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.