gpt4 book ai didi

python - 无法在 View 中获取 Ajax Post 数据

转载 作者:行者123 更新时间:2023-12-01 01:27:37 27 4
gpt4 key购买 nike

我正在发送一个查看的帖子请求。一切都很好,但ajax没有发送数据。在ajax调用中我检查了变量是否存在数据,但在 View 中发送数据时不存在。

我的ajax是

  $.post({
url: '/projpost/message/{{project.id}}',
data: {
'message': message
},
success: function (data) {
alert(message); //working perfect


}
});

在 View 中

@csrf_exempt
def message(request,id):
print(request.POST['message'])
return HttpResponse(content_type="application/json" )

但是我收到错误

MultiValueDictKeyError at /projpost/message/5/
'message'

最佳答案

尝试:

@csrf_exempt
def message(request,id):
print(request.POST.get('message'))
# or print(request.POST.get('message', ''))
return HttpResponse(content_type="application/json" )

但是,如果您发现 request.POST 为空,那么您将不得不仅处理 request,而不是 request.POST

关于python - 无法在 View 中获取 Ajax Post 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53215885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com