首页 > Web开发 > 详细

ajax 前端发含有列表的数据

时间:2018-12-16 11:43:28      阅读:129      评论:0      收藏:0      [点我收藏+]

在前端页面也可以给后端发送一个包含列表的数据

html

<body>
<h3>index页面 </h3>

<input type="text" name="cal_1">+
<input type="text" name="cal_2">=
<input type="text" name="cal_3">
<button id="b2">计算</button>
<br>
<button id="b3">测试的数据</button>

<script src="/static/js/jquery.js"></script>


<script>

   $("#b3").click(function () {
        $.ajax({
            url: "/ajax_test/", //往这个地址发
            type: "post",
            data: {
                name: "alex",
                age: "33",
                hobby: JSON.stringify(["抽烟", "喝酒", "烫头"])//传多个值的时,用json字符串
            },
            success: function (res) {
                $("[name=‘cal_3‘]").val(res)

            },
        })
    })

</script>

是前端发送的数据中为什么没有csrftoken呢???

views视图部分

 

def ajax_test(request):
    print(request.POST)
    print(request.POST.get("name"))
    print(request.POST.get("age"))

    hobby=request.POST.get("hobby")
    print(json.loads(hobby))# 把接受的json字符串反序列化

    return HttpResponse("ajax test")

 

ajax 前端发含有列表的数据

原文:https://www.cnblogs.com/kenD/p/10125989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!