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 src="/static/js/ajax_setup.js"></script>  從請求頭中獲取64位的cookie值 是在全局范圍內加的 有這個文檔之后所有的ajax請求都不需要額外再加csrftoken

 

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

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

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM