4

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 comes to tmp3 since it is an array, it is printing the first element only.

3
  • Did you try request.form.get('col_filter') Commented Oct 14, 2015 at 9:10
  • Yes. It is giving me null value when I print it using console.log(data.otstr) Commented Oct 14, 2015 at 9:13
  • Similar: http://stackoverflow.com/a/24808706/3710490 Commented Oct 14, 2015 at 9:22

1 Answer 1

7

You need to retrieve col_filter as a list:

tmp3 = request.form.getlist('col_filter[]')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.