Я создаю отдельный REST-API Django для моего приложения ReactJS.Я вызываю API POST для своей конечной точки, чтобы зарегистрировать пользователей.Я не уверен, что означает ошибка, поскольку я получаю статус 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 в консоли.