简体   繁体   中英

how to send variable from ajax to FLASK python?

I'm new in python, now I want use send variable from ajax to python with FLASK.

My html

$(document).ready(function(){  
    $('#submit_button').click(function(){
        if (confirm("Are you sure you want to Process this form?")){
            var form=$('#form2').serialize();
            $.ajax({
                url:"http://localhost:5000/",
                method:"POST",
                data:form,
                success:function(){
                    $('#form2')[0].reset();
                    alert('Process Success');
                } 
            });
        }
    });
});

Python

from flask import Flask
app = Flask(__name__)
'''def round_up(n, decimals=0): 
    multiplier = 10 ** decimals 
    return math.ceil(n * multiplier) / multiplier'''
@app.route('/', methods=['GET','POST'])
def main():
    clicked=None
    if request.method == "POST":
        clicked=request.json['data']
    return render_template('test.html')

if __name__ == "__main__": 
    main()

Can somebody help me with this?

Regards, Andri

CORS is not a programming language bounded thing, it is more general than this. You can check this video for better understanding. After that you can check this link which will help you to manage CORS for your Flask application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM