我正在为我的ReactJS应用程序创建一个单独的Django REST-api。我正在向端点调用访存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。
答案 0 :(得分:0)
错误[Errno 54] Connection reset by peer
表示加载资源(在此示例中为ajax响应)已中断,但不是由于服务器故障。这意味着连接断开或客户端中断了该资源的加载。
对于您而言,这可能意味着您的JavaScript代码或浏览器本身有问题。在浏览器调试器中检查“网络”标签,以找到该中断的连接,这应该有助于对其进行跟踪。