zoukankan      html  css  js  c++  java
  • django 如何接收bootstrap-table传送的 ajax数组

     今天在用django传递id的时候,使用 alert(ids)以及console.log("id:",ids),都可以看到是把选中的数据的id打印出来的,用console.log可以看到他是一个数组显示

    $(function () {
        $button.click(function () {
            var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.id;
            });
            alert(ids)
            console.log("id:",ids)
            $.ajax({
                url:"{{ request.path }}",
                type:'POST',
                dataType: 'json',
                data:{'id':ids,'action':'batch_delete'},
                success: function(callback){
                    console.log(callback);
    
                },
                error: function(){
                    alert("fail")
                }
            })
            $table.bootstrapTable('remove', {
                field: 'id',
                values: ids
            });
        });
    });

     

    但是在后端的django却没有任何的显示,这个到底是怎么一回事情呢

        if request.is_ajax() and request.method == 'POST':
            print("ids",request.POST.getlist("id"))
            print("action", request.POST.get("action"))

     --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    解决方案在js代码中加入traditional:true 就可以传递数组了

    $(function () {
        $button.click(function () {
            var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.id;
            });
            alert(ids)
            console.log("id:",ids)
            $.ajax({
                url:"{{ request.path }}",
                traditional:true,    //加上此项可以传数组,传递数组必须要加,否则数组无法传递到后端
                type:'POST',
                dataType: 'json',
                data:{'id':ids,'action':'batch_delete'},
                success: function(callback){
                    console.log(callback);
    
                },
                error: function(){
                    alert("fail")
                }
            })
            $table.bootstrapTable('remove', {
                field: 'id',
                values: ids
            });
        });
    });

     在后台我们看到现在的ids是能够传递过来了

  • 相关阅读:
    Ui——创建视图的方法及过程
    iOS设计模式----原型模式
    浅谈OC中排序的方法
    Solid Dominoes Tilings (轮廓线dp打表 + 容器)
    Shell Necklace (dp递推改cdq分治 + fft)
    Rigid Frameworks (画图二分图规律 + DP + 数学组合容斥)
    PowMod (欧拉推式子 + 指数循环节)
    Necklace (全排列 + 匈牙利)
    GCD (RMQ + 二分)
    Game (思维)
  • 原文地址:https://www.cnblogs.com/qinhan/p/9116989.html
Copyright © 2011-2022 走看看