ajax 前端发含有列表的数据

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

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")

猜你喜欢

转载自www.cnblogs.com/kenD/p/10125989.html