简体   繁体   中英

Sending data back from Javascript to Python

I am very new to Python / Flask / Javascript, and would appreciate it if someone could help me with this.

I am trying to pass a string value from Javascript back to Python but it does not seem to work. My code is as follows:

Javascript (partial code):

var bounds = '8.3,9,34.3,15.9';
$.post("/receiver", {
    bbox_bounds: bounds
});

In Python:

from flask import Flask, request

#Create the Flask app
app = Flask(__name__)

@app.route('/receiver', methods = ['POST'])
def receiver():
    bbounds = request.form('bbox_bounds')
    print "Bounds: " + bbounds
    return bbounds



if __name__ == '__main__':
    app.run(port=5000)

Sorry, the above is bad, but I am really a beginner. Thank you.

Change this line,

bbounds = request.form('bbox_bounds')

to

bbounds =request.form['bbox_bounds']

Refer to the Flask Quickstart guide for more detailed examples - http://flask.pocoo.org/docs/1.0/quickstart/#routing

Also issues as such can be easily caught and fixed if you look into your logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM