I've been trying to figure out the best way to pass data from frontend to backend in Flask. The problem is that when receiving data on the back end, the data retrieval came from the request.form['msg'], which doesn't make sense since I didn't really use a form, just an input field. Why did it do this? Is there a better way to retrieve data?
Here's my code. There's also an index.html with <input id='msg'> </input>
jQuery / Javascript:
const message = document.getElementById('msg');
$(document).load('/run', {'msg': message.value}, function(){return 'run complete'})
main.py:
@app.route("/run", methods=["GET", "POST"])
def run():
if request.method == 'POST':
msg = request.form["msg"]
print('msg:', msg)
return "OK"
I'll just reenumerate what I know to pass from front-end to backend.
1. request.form["name_of_the_input"]
HTML
Python
2. Ajax/Fetch
JavaScript
Python
3. Flask-SocketIO (https://flask-socketio.readthedocs.io/en/latest/)
Example code
Python
JavaScript
4. request.args (Flask request.args.get get all params (Python))
5. Same functionality as above but
Python
So when 127.0.0.1:5000/hello/anything_else
data = "anything _else"