0

So I want to send an array to a python script using flask.

Here's my relevant javascript:

var blacklist = [1];
function getSents() {
 $.ajax({
    type: "POST",
    url: "script.py",
    data: {'blacklist':blacklist},
    dataType: 'json',
    success: function(response) {
        // do something
        },
    error: function (xhr, status, error) {
        console.log(error);
        }
});
}

And in flask, I have tried every Attribute of request that I found. When printed, it always gives an empty bytes string.

If I use

json.loads(request.data)

It raises an error because request.data is empty.

Relevant python code:

from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
application = Flask(__name__)

@application.route('/', methods=['GET', 'POST'])
def something():
    blacklist = request.get_data()
    # do something

if __name__ == '__main__':
    application.run(debug=True)
Smogshaik
  • 180
  • 2
  • 13

1 Answers1

-1

Try setting contentType to 'application/json' and stringify your data property. Then check request.json in flask

var blacklist = [1];
function getSents() {
 $.ajax({
    type: "POST",
    url: "script.py",
    data: JSON.stringify({'blacklist':blacklist}),
    contentType: "application/json",
    dataType: 'json',
    success: function(response) {
        // do something
        },
    error: function (xhr, status, error) {
        console.log(error);
        }
});
}

If you are looking for a starting place to build from this should work

under app.py

from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
application = Flask(__name__, static_url_path='/static')

@application.route('/', methods=['GET', 'POST'])
def something():
    if request.method == 'GET':
        return application.send_static_file('index.html')
    blacklist = request.json
    print(blacklist)
    return jsonify(blacklist)

if __name__ == '__main__':
    application.run(debug=True)

then under /static/index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    </head>
    <body>
       <script>
        var blacklist = [1];
        function getSents() {
         $.ajax({
            type: "POST",
            url: "script.py",
            data: JSON.stringify({'blacklist':blacklist}),
            contentType: "application/json",
            dataType: 'json',
            success: function(response) {
                // do something
                console.log(response)
                },
            error: function (xhr, status, error) {
                console.log(error);
                }
        });
        }
        getSents();
        </script>
    </body>
</html>
Mike
  • 587
  • 3
  • 6
  • Doesn't seem to work. Same behavior as before, unfortunately. – Smogshaik Jan 15 '19 at 00:37
  • The only other issues I could potentially see based off you flask code could be: your url in the ajax request is script.py, instead of the url to your flask endpoint, your not returning a response from your flask route which will throw an error, or your not serving your content from another address you will run into a cors issue. Hope that helps! – Mike Jan 15 '19 at 04:05