无法在Django视图中获取http POST数据

时间:2021-05-25 18:20:45

I'm using Angular to post some data to a Django endpoint with:

我用角向Django端点发送一些数据:

$http({
  method: 'POST',
  url: '/api/projects/166/interval_sort/',
  data: JSON.stringify({ids: [251,250,249,235,233,234]})
})

And in my view method, request.POST is an empty <QueryDict: {}>

在我的视图方法中,请求。POST是一个空的 {}>

def ProjectIntervalSort(request, pk):
  logger.debug("SORTABLE DATA: "+str(request.POST))
  return HttpResponse(status=204)

Chrome's network inspector shows my JSON object being sent, but I don't seem to be able to access it from the Django side. Am I not formatting the data correctly or missing something in my view method? Thanks.

Chrome的网络检查器显示了我的JSON对象被发送,但是我似乎无法从Django方面访问它。我没有正确地格式化数据,还是在视图方法中漏掉了什么?谢谢。

1 个解决方案

#1


3  

request.POST is for form-encoded data. You're using JSON, and you can find it in request.body.

请求。POST用于表单编码的数据。您正在使用JSON,可以在request.body中找到它。

#1


3  

request.POST is for form-encoded data. You're using JSON, and you can find it in request.body.

请求。POST用于表单编码的数据。您正在使用JSON,可以在request.body中找到它。