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.

3
  • 1
    "but it does not seem to work" Are you getting any errors anywhere? Commented Jun 6, 2018 at 16:59
  • 1
    Can you specify what you mean by "does not seem to work"? Is there an error message? Unexpected output? Commented Jun 6, 2018 at 16:59
  • I checked the Developer tools Console and noticed: ReferenceError: $ is not defined. So I realized I needed to include <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 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. Commented Jun 7, 2018 at 15:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 stackoverflow.com/questions/22181384/…. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.