无法将数据从JS AJAX发送回Django

编程入门 行业动态 更新时间:2024-10-28 04:15:26
本文介绍了无法将数据从JS AJAX发送回Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

html-javascript

html-javascript

var csrftoken = $('[name="csrfmiddlewaretoken"]').val(); $('#Save').click(function () { var ajaxdata = { exam: $('#Exam').val() }; $.ajax({ url: '/save', type: 'POST', dataType: 'json', data: JSON.stringify(ajaxdata), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', "X-CSRFToken": csrftoken }, credentials: 'include', success: function () { alert(ajaxdata); console.log(ajaxdata); }, error:function (xhr, ajaxOptions, thrownError){ console.log(ajaxdata); } }); }

views.py

import json from django.http import HttpResponse def save(request=request): data = json.loads(request.body) testexam = data['exam'] testexam = request.POST.get('exam') testobj = MyObject.objects.filter(name="David").first() testobj.Exam = testexam testobj.save() return HttpResponse(json.dumps({"success": True}), content_type="application/json")

这些是我现在的html和视图.

These are my html and views now.

删除了烧瓶"部分.

data = json.loads(request.body)

允许我成功接收数据!

还是不太明白为什么"request.POST"不起作用.

Still don't quite understand why " request.POST " didn't work.

如果我设法知道原因,我会稍后再回来更新!

I'll come back later to update if I manage to know the reason!

感谢您的评论和有用的建议!

Thanks for the comments and useful suggestions!

推荐答案

无需使用烧瓶.

这是我接收数据的方式:

This is how I receive the data:

data = json.loads(request.body)

一切正常!

感谢所有评论,建议和答案!非常感谢!

Thanks for all the comments, suggestions, answers! Really appreciated!

更多推荐

无法将数据从JS AJAX发送回Django

本文发布于:2023-11-11 11:13:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   JS   Django   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!