{ "detail": "JSON 解析错误 - 期望值:第 1 行第 1 列(字符 0)" }

新手上路,请多包涵

我正在使用 django-restframework,我使用 postman POST json 数据到我的项目但是我得到了像 tittle 这样的错误,我已经设置了 raw 和 application/json 这里是来自 postman 的代码。

 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
}

我的代码真的很简单,就像:

 from rest_framework.parsers import JSONParser,ParseError

class ReplyViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list` and `detail` actions.
    """

    pagination_class=PageNumberPagination
    queryset = Forum_reply.objects.all()
    serializer_class = ReplySerializer

    #filter
    filter_backends = (DjangoFilterBackend, )
    filter_fields = ['postID',]
    def create(self, request, *args, **kwargs):
        print(request.data)
        data = JSONParser().parse(request)
        return HttpResponse("ok")

使用视图集后,出现此错误,我已将其打印在外壳上,但没问题

原文由 Michael Porter 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 467
1 个回答

我实际上遇到了同样的错误,如果您使用的是 ajax,则需要像下面这样使用 JSON.stringify(data):

 datato= {
    "id" : 3,
    "title" : "level up",
    "author" : "jason rock"

}

$.ajax({
    method:'POST',
    url:"/home/api",
    data : JSON.stringify(datato),

})

@SeriForte 的回答为我指明了正确的方向

原文由 El Bachir 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题