javascript - send array from ajax to Flask not working - Stack Overflow

I am trying to send an array to flask through ajax post call. But somehow it is not working.Javascript&

I am trying to send an array to flask through ajax post call. But somehow it is not working.

Javascript

<script type="text/javascript">
        function fillChart()
        {

            var nids = document.getElementById("nodes-select").value;
            var cfilter = document.getElementById("filter-select").value;
            var chkd = document.getElementById("further-select");
            var cids = [];
            for (var i=0;i<chkd.length;i++)
            {

                if(chkd[i].selected)
                {
                    cids.push(chkd[i].value);
                }
            }

            alert(cids);
            $.post("/pie",{"node_id":nids,"col_select":cfilter,"col_filter":cids},function(data,status)
            {
                var tmp = data;            
                console.log(data.otstr);                     

            });           
        }
 </script>

Server code

@app.route('/pie',methods=['POST'])
def pie():
    tmp1 = request.form.get('node_id')  
    tmp2 = request.form.get('col_select')   
    tmp3 = request.form.get('col_filter[]') 
    return jsonify(otstr=[tmp1,tmp2,tmp3])

Here tmp1 and tmp2 are just strings and tmp3 is an array of strings.console.log(data.otstr) is printing correct values of tmp1,tmp2 but when it es to tmp3 since it is an array, it is printing the first element only.

I am trying to send an array to flask through ajax post call. But somehow it is not working.

Javascript

<script type="text/javascript">
        function fillChart()
        {

            var nids = document.getElementById("nodes-select").value;
            var cfilter = document.getElementById("filter-select").value;
            var chkd = document.getElementById("further-select");
            var cids = [];
            for (var i=0;i<chkd.length;i++)
            {

                if(chkd[i].selected)
                {
                    cids.push(chkd[i].value);
                }
            }

            alert(cids);
            $.post("/pie",{"node_id":nids,"col_select":cfilter,"col_filter":cids},function(data,status)
            {
                var tmp = data;            
                console.log(data.otstr);                     

            });           
        }
 </script>

Server code

@app.route('/pie',methods=['POST'])
def pie():
    tmp1 = request.form.get('node_id')  
    tmp2 = request.form.get('col_select')   
    tmp3 = request.form.get('col_filter[]') 
    return jsonify(otstr=[tmp1,tmp2,tmp3])

Here tmp1 and tmp2 are just strings and tmp3 is an array of strings.console.log(data.otstr) is printing correct values of tmp1,tmp2 but when it es to tmp3 since it is an array, it is printing the first element only.

Share Improve this question asked Oct 14, 2015 at 9:01 krishkrish 1671 gold badge3 silver badges12 bronze badges 3
  • Did you try request.form.get('col_filter') – Valijon Commented Oct 14, 2015 at 9:10
  • Yes. It is giving me null value when I print it using console.log(data.otstr) – krish Commented Oct 14, 2015 at 9:13
  • Similar: http://stackoverflow./a/24808706/3710490 – Valijon Commented Oct 14, 2015 at 9:22
Add a ment  | 

1 Answer 1

Reset to default 7

You need to retrieve col_filter as a list:

tmp3 = request.form.getlist('col_filter[]')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742377465a4432474.html

相关推荐

  • javascript - send array from ajax to Flask not working - Stack Overflow

    I am trying to send an array to flask through ajax post call. But somehow it is not working.Javascript&

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信