首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将JavaScript变量值传入python变量(Flask)

如何将JavaScript变量值传入python变量(Flask)
EN

Stack Overflow用户
提问于 2019-03-01 08:15:15
回答 2查看 1.1K关注 0票数 1

我想在python变量中发送这个变量值,以执行不同的任务。

代码语言:javascript
复制
var d="string"

我不想通过URL发送变量值。我想使用一些像这样的代码。

代码语言:javascript
复制
@app.route("/", methods=['POST'])
def hello1():
    d = request.form['n'] #here i take value from name of html element
EN

回答 2

Stack Overflow用户

发布于 2019-03-01 08:23:57

使用AJAX帖子。

代码语言:javascript
复制
let myVar = 'Hello'
$.post('http://localhost:5000/getjs', {myVar}, function(){
    // Do something once posted.
})

你的瓶子会是这样的

代码语言:javascript
复制
@app.route('/getjs', methods=['POST'])
def get_js():
    if request.method == 'post':
        js_variable = request.form
        return js_variable

或者,您可以执行以下操作:

代码语言:javascript
复制
@app.route('/getjs/<variable>')
    def get_js(variable):
        js_variable = variable
        return js_variable

所以当你把你的网址指向苹果的时候,js_variable将会是‘http://localhost:5000/getjs/apples

票数 1
EN

Stack Overflow用户

发布于 2019-03-03 05:08:05

使用原生javascript 'fetch‘。

Ari Victor的解决方案需要一个外部库: jquery

代码语言:javascript
复制
var url = 'http://localhost:5000/getjs';
var data = {field_name: 'field_value'};

fetch(url, {
  method: 'POST',
  body: JSON.stringify(data), 
  headers:{
    'Content-Type': 'application/json'
  }
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54936206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档