我想在 python 变量中发送这个变量值来执行不同的任务。
var d="string"
我不想通过 URL 发送变量值。 我想使用一些像这样的代码。
@app.route("/", methods=['POST'])
def hello1():
d = request.form['n'] #here i take value from name of html element
最佳答案
使用 AJAX 帖子。
let myVar = 'Hello'
$.post('http://localhost:5000/getjs', {myVar}, function(){
// Do something once posted.
})
你的 Flask 将会是这样的
@app.route('/getjs', methods=['POST'])
def get_js():
if request.method == 'post':
js_variable = request.form
return js_variable
或者您可以这样做:
@app.route('/getjs/<variable>')
def get_js(variable):
js_variable = variable
return js_variable
所以当您将网址定向到 http://localhost:5000/getjs/apples 时 js_variable 将是“苹果”
关于javascript - 如何将 JavaScript 变量值传递给 python 变量(flask),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936206/