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是能够传递过来了

  • 相关阅读:
    由二进制移位想到的
    KDJ指标详解
    PMP考试结束
    转K线理论初级二
    日本地震效应
    Baseline之流水先生的见解
    KDJ判断原则
    转K线理论初级一
    管理学法则
    今天提到KW,特此@Mark一下
  • 原文地址:https://www.cnblogs.com/qinhan/p/9116989.html
Copyright © 2011-2022 走看看