How to Work With AJAX Request FLASK code example
Example 1: how to create ajax api in flask
<script type=text/javascript src="{{
url_for('static', filename='jquery.js') }}"></script>
Example 2: how to get data in flask from ajax
from flask import *
app = Flask(__name__)
@app.route("/", methods=["POST", "GET"])
def home():
return render_template("index.html")
# This function gets param1 from url yourflaskroot/getdata
# which was the url set in ajax code and return param1 back to it
# you can write a simple ajax code to test it out
@app.route("/getdata")
def myFunction():
# Fill parameter with the argument sent from ajax
parameter = request.args.get('param1')
# Send parameter as an argument to ajax
return f"the returned data from python is : {parameter}"
if __name__ == "__main__":
app.run(debug=True)