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

后端 未结 2 962
礼貌的吻别
礼貌的吻别 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条回答
  •  萌比男神i
    2020-12-06 07:52

    Django request can only parse application/x-www-form-urlencoded and multipart/form-data to request.POST. For other content types you have to use request.body property. for assertion of content type you can get the content type from request.META.get('CONTENT_TYPE')

    def sample_view(request):
        if request.META.get('CONTENT-TYPE') == 'application/json':
            data = json.loads(request.body)
            return JsonResponse({'any-message':'Hello'})
    

提交回复
热议问题