Search on blog:

Python: How to use AJAX in Flask using jQuery.

After all there was two problems:

  • HTML wasn't serve from flask/server. And browser could block it for security reason. I put HTML directly in function index() but later you can use render_template("filename.html")

  • you forgot to load javascript jQuery library.

Other: Flask - Calling python function on button OnClick event

from flask import Flask, jsonify
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app, support_credentials=True)

def index():
    return """
<script src="//"></script>

<script type=text/javascript>
   $(function() {
     $('a#sender').bind('click', function() {
         function(data) {
       return false; // stop <a> to send normal request

    <a href=# id="sender"><button>Send AJAX</button></a>


def background_process_test():
    print("Hello AJAX")
    return jsonify({'success': 'OK'})

if __name__ == "__main__":'', port=8000)


Stackoverflow Problem with Ajax and flask, what is the problem?

If you like it
Buy a Coffee