1

This is my javascript code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
    $(function() {
        $.ajax({
            type: 'POST',
            url: "/jstoflask",
            data: {
                1: "Welcome",
                2: "to",
                3: "Geeks",
                4: "for",
                5: "Geeks"
            },

            success: function(data) {
                console.log('success', data);
            }
        });
    });
</script>

This is my flask code

@app.route('/jstoflask',methods=["POST"])
def jstoflask():
    data=request.form('data')
    print(data)  
    return jsonify(data)

I'm getting an error : TypeError: 'ImmutableMultiDict' object is not callable. What am I doing wrong? How do I solve this?

Varshh KS
  • 41
  • 1
  • data=request.form('data') i suspect your problem is in this line. You are not sending any form or field with name "data" – Pharsa Thapa Jul 12 '20 at 14:33
  • have a look at this thread https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request it may help you – cizario Jul 12 '20 at 16:08

1 Answers1

0

It's because request.form is not a method you can call. It's the payload as an ImmutableMultiDict. You can change the line to data = request.form and you'd be able to see the value.

Since the request object is a dictionary, you can access the data by passing a key like so: request.form['1'].

Official Docs

alexako
  • 122
  • 7