0

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.

hmmwv
  • 21
  • 1
  • 1
    _"but it does not seem to work"_ Are you getting any errors anywhere? – Luca Kiebel Jun 06 '18 at 16:59
  • 1
    Can you specify what you mean by "does not seem to work"? Is there an error message? Unexpected output? – Linuxios Jun 06 '18 at 16:59
  • I checked the Developer tools Console and noticed: `ReferenceError: $ is not defined`. So I realized I needed to include `` in the header and this error stop. However, it is still not running and some other parts of the script is giving error. I suspect I have not done my above javascript and python script correctly to post and handle the data. Any help please? Thank you. – hmmwv Jun 07 '18 at 15:49

1 Answers1

1

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.

avizzzy
  • 440
  • 9
  • 21
  • Hi! I did the change as you mentioned, but it is still not working. I suspect my own javascript and python code is bad (see above comment). Any help please? Thank you. – hmmwv Jun 07 '18 at 15:54
  • Hi, I realized I had multiple problems in my earlier code, one of which is "No 'Access-Control-Allow-Origin' header is present on the requested resource". I managed to get a solution from https://stackoverflow.com/questions/22181384/javascript-no-access-control-allow-origin-header-is-present-on-the-requested. Thank you for your help. – hmmwv Jun 10 '18 at 10:04