【问题标题】:how to send variable from ajax to FLASK python?如何将变量从 ajax 发送到 FLASK python?
【发布时间】:2020-07-27 06:27:37
【问题描述】:

我是 python 新手,现在我想用 FLASK 将变量从 ajax 发送到 python。

我的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()

有人可以帮我吗?

问候, 安德里

【问题讨论】:

  • 你或多或少在做。你得到了什么,这段代码有什么问题?
  • @deceze 从源“null”访问“localhost:5000”的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。

标签: python ajax flask


【解决方案1】:

CORS 不是编程语言有界的东西,它比这更通用。您可以查看this 视频以获得更好的理解。之后,您可以查看this 链接,该链接将帮助您管理 Flask 应用程序的 CORS。

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多