Mic*_*ter 5 django json python-3.x django-rest-framework
我正在使用 django-restframework,我使用邮递员 POST json 数据到我的项目,但我收到类似标题的错误,我已经设置了 raw 和 application/json 这里是邮递员的代码。
POST /account/post/reply/ HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: a0c7bd93-631e-4c7a-8106-87f018eaf7da
{
"user": "michael",
"userid": "1",
"ihelpid": 6,
"tittle": "6",
"info": "6",
"label": "3",
"tel": "dxy970525",
"picture1": null,
"picture2": null
}
Run Code Online (Sandbox Code Playgroud)
我的代码非常简单,就像:
POST /account/post/reply/ HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: a0c7bd93-631e-4c7a-8106-87f018eaf7da
{
"user": "michael",
"userid": "1",
"ihelpid": 6,
"tittle": "6",
"info": "6",
"label": "3",
"tel": "dxy970525",
"picture1": null,
"picture2": null
}
Run Code Online (Sandbox Code Playgroud)
我使用viewsets后,出现此错误,我已将其打印在shell上,但没有问题
我实际上遇到了同样的错误,如果您使用 ajax ,则需要使用 JSON.stringify(data) ,如下所示:
datato= {
"id" : 3,
"title" : "level up",
"author" : "jason rock"
}
$.ajax({
method:'POST',
url:"/home/api",
data : JSON.stringify(datato),
})
Run Code Online (Sandbox Code Playgroud)
@SeriForte 的回答为我指明了正确的方向
我已经解决了这个问题,现在可以访问数据了
我更改了旧代码
print(request.data)
data = JSONParser().parse(request)
Run Code Online (Sandbox Code Playgroud)
这会出错。如果我像下面这样编码:
print(request)
data = JSONParser().parse(request)
Run Code Online (Sandbox Code Playgroud)
然后我就可以访问字典中的数据。
所以,我不知道为什么,但问题已解决
| 归档时间: |
|
| 查看次数: |
31398 次 |
| 最近记录: |