javascript - 如何将 JavaScript 变量值传递给 python 变量(flask)

标签 javascript python html flask

我想在 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/

相关文章:

html - 如何将每 3 个 div 的内容包装在一个 div 中?

html - 如何使用 css 定位和设置 Google recaptcha 的样式?

javascript - ESLint:如何在文件中设置 "new-cap"规则的 "capIsNewExceptions"选项?

python - 找不到 SQL 服务器

python - 访问 python3 关联数组中的数据

python - requests/httplib/urllib 返回 3 个不同的结果

javascript - jQuery - 单击链接后无法显示下拉列表

javascript - Jquery获取请求Uri太长

javascript - 如何选择 node_modules dist flavor 与 webpack 捆绑在一起

javascript - 如何从 JSON Schema 默认构造 JavaScript 对象?