Call python function from JS

后端 未结 2 798
梦如初夏
梦如初夏 2020-11-30 02:05

I am trying to call a function in Python from my JavaScript code. I used the code explained here but it does not work for me.

Here is my JS code:

&l         


        
2条回答
  •  清歌不尽
    2020-11-30 02:41

    After searching for few hours, I ended up having the following, and it worked well. Hope this helps someone else in future.

    HTML and JS code: loging.html:

    
     
        Flask Intro - login page
        
        
          
     
     
        
        
        
    

    Python code: app.py:

    from flask import Flask, render_template, redirect, url_for,request
    from flask import make_response
    app = Flask(__name__)
    
    @app.route("/")
    def home():
        return "hi"
    @app.route("/index")
    
    @app.route('/login', methods=['GET', 'POST'])
    def login():
       message = None
       if request.method == 'POST':
            datafromjs = request.form['mydata']
            result = "return this"
            resp = make_response('{"response": '+result+'}')
            resp.headers['Content-Type'] = "application/json"
            return resp
            return render_template('login.html', message='')
    
    if __name__ == "__main__":
        app.run(debug = True)
    

提交回复
热议问题