如何解决 django rest-framework 中的错误请求
Posted
技术标签:
【中文标题】如何解决 django rest-framework 中的错误请求【英文标题】:how to solve bad request in django rest-frame-workd 【发布时间】:2020-12-04 21:54:53 【问题描述】:我创建了一个接受用户电子邮件和密码登录的 api。当我尝试访问 api 时,我收到了错误的请求。我一直在寻找需求,查看我的代码。但我无法弄清楚我做错了什么。请帮忙
在我的 html 表单下方
<form id="loginform"action="userlogin/" method="post" enctype="multipart/form-data">
<div class="errors-field">
</div>
<div class="input-field">
<label >
Email
</label>
<input placeholder="Email"type="email" name="email" value=""required>
</div>
<div class="input-field">
<label>
Password
</label>
<input placeholder="Password"type="password" name="password" value=""required>
</div>
<button type="submit">Login</button>
</form>
下面是我的java脚本
const lgform = document.querySelector('#loginform')
lgform.addEventListener('submit',loginForm)
function loginForm(event)
event.preventDefault()
const myform = event.target
const myFormData = new FormData(myform)
const url = myform.getAttribute("action")
const method = myform.getAttribute("method")
const csrftoken = getCookie('csrftoken')
const xhr = new XMLHttpRequest()
xhr.open(method,url)
xhr.setRequestHeader("Content-Type","application/json")
xhr.setRequestHeader("HTTP_X_REQUEST_WITH","XMLHttpRequest")
xhr.setRequestHeader("X-Requested-with","XMLHttpRequest")
xhr.setRequestHeader("X-CSRFToken",csrftoken)
xhr.onload=function()
const serverResponse = xhr.response
const serverData = JSON.parse(serverResponse)
console.log(serverData.email)
if(xhr.status==200)
if(serverData.token)
cname = "user"
cvalue = serverData.token
exdays = 1
setCookie(cname, cvalue, exdays)
xhr.onerror=function()
console.log('error')
xhr.send(myFormData)
我的序列化器
class loginUserSerializer(serializers.Serializer):
password = serializers.CharField(style='input_type':'password',write_only=True,required=True)
email = serializers.EmailField(required=True)
def validate_email(self,value):
data = self.get_initial()
pw = data.get('password')
email= value
user=authenticate(email=email,password=pw)
if not user or not user.is_active:
raise serializers.ValidationError('email or password not correct')
return value
我的 api 视图
class loginUser(ObtainAuthToken):
def post(self,request,*args,**kwargs):
data =
serializer = loginUserSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
email = serializer.validated_data['email']
password = serializer.validated_data['password']
user = authenticate(email=email,password=password)
if user is not None and user.is_active:
try:
usertoken = Token.objects.get(user=user)
data ='token':usertoken.key
print(usertoken)
except:
pass
else:
data =serializer.errors
return Response(data,status=200)
我在提交表单时收到 api bad request。请我需要弄清楚我做错了什么。提前致谢。
在我的控制台日志中,当我打印出服务器响应数据时,您可以看到如下
Object detail: "JSON parse error - Expecting value: line 1 column 1 (char 0)"
【问题讨论】:
检查API请求的response。它应该包含有关错误请求的相关信息。 好吧,正如你在我的代码中看到的,我错过了什么? 【参考方案1】:我不应该包括
xhr.setRequestHeader("Content-Type","application/json")
在我的标题中
【讨论】:
以上是关于如何解决 django rest-framework 中的错误请求的主要内容,如果未能解决你的问题,请参考以下文章
如何解决“'mathfilter'标签'addition'在复制django项目后停止工作”错误?
如何解决这个 django-private-chat 错误?