Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python flask request returns undefined values

I want to pass array to Python Flask but I got empty result or b'undefined=&undefined=&undefined='. Here is my code Javascript

var test = [1, 2, 3];
  $.ajax({
        url: '/table',
        data : test,
        type: 'POST',
        success: function(response) {
            console.log(response);
        },
        error: function(error) {
            console.log(error);
        }
    });

and Python code

app.route('/table', methods = ['POST'])
def table():
    #print(request.values)
    print(request.get_data())
    return 'got this'
like image 433
Mr Lukas Avatar asked Apr 03 '26 03:04

Mr Lukas


1 Answers

You need to use JSON to send back values that are arrays, objects, etc in javascript:

var test = [1, 2, 3];
$.ajax({
    url: '/table',
    data : {'payload':JSON.stringify(test)},
    type: 'get',
    success: function(response) {
        console.log(response);
    },
    error: function(error) {
        console.log(error);
    }
});

Then, in the app:

import json
@app.route('/table')
def table():
  _result = json.loads(flask.request.args.get('payload'))
  return 'got this'
like image 113
Ajax1234 Avatar answered Apr 04 '26 17:04

Ajax1234



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!