【发布时间】:2019-09-22 00:28:01
【问题描述】:
我想通过 Ajax 将数据发送到我的 Django 服务器。但是当我想读取这些数据时,我得到了一个错误的请求错误。我不知道为什么,其他方法如 request.method 或 request.stream 效果很好。
这是 Ajax 调用:
jQuery.ajax({
type: 'post',
url: 'http://127.0.0.1:8000/termin/get-journey-time/',
contentType: 'application/json; charset=utf-8',
data: {
'method': 'get_journey_time',
'mandant_id': 1,
'customer_address': customer_address,
'staff_group': staff_group_id
},
success: function (resultData) {
console.log(resultData)
});
这是我对这个帖子请求的看法:
class get_journey_time(generics.ListAPIView):
"""
Handle Ajax Post to calculate the journey time to customer for the selected staff group
"""
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
body = request.data
return Response(body)
我收到错误代码 400 错误请求。你知道为什么吗?
def post(self, request, *args, **kwargs):
body = request.method
return Response(body)
或
def post(self, request, *args, **kwargs):
body = request.stream
return Response(body)
好好工作
【问题讨论】:
-
你试过
from django.http import JsonResponse吗? -
日本但这里一样
-
在
post()中放一个print("foo")函数来检查请求是否命中 -
print('foo')works,以及 request.method 或 request.stream -
可以添加错误回溯吗?
标签: javascript python ajax django django-rest-framework