Ajax POST not sending data when calling 'request.POST' in Django view

后端 未结 2 945
礼貌的吻别
礼貌的吻别 2020-12-06 07:31

I am attempting to get an Ajax POST to to send data to my view so I can manipulate my data there, when I click on a div with class up-arrow

2条回答
  •  無奈伤痛
    2020-12-06 07:41

    When posting JSON data with application/json you need to use request.body instead of request.POST.

    Like so:

    class VoteUpPost(View):
        def post(self, request):
            print(request.body)
            data = json.loads(request.body)
            return JsonResponse({'status': True})
    

    Also as Jacques mentioned, make sure to update your js to pass a JSON string.

    Change:

    data: {'dane': 123456789101112},
    

    To:

    data: JSON.stringify({'dane': 123456789101112}),
    

提交回复
热议问题