【问题标题】:How can I pass a dictionary from HTML to a Flask Python method? [duplicate]如何将字典从 HTML 传递到 Flask Python 方法? [复制]
【发布时间】:2020-09-14 21:37:09
【问题描述】:

我有一个 HTML 文件中的字典。我希望能够以“xlsx”(Excel)格式下载这些数据。我有将任何 Python 字典转换为新转换的 Excel 文件的代码,但我不知道如何将字典从 HTML 传递到 Python/Flask 路由方法。

【问题讨论】:

  • 你真的不能......你应该使用js从你在html中设置的数据生成一个json......调用一个url(这是你的python/flask服务器)

标签: python html flask


【解决方案1】:

Flask Route 获取数据。

@app.route('/postmethod', methods = ['POST'])
def get_post_javascript_data():
    jsdata = request.get_json() # parse as json

AJAX 发送 JSON 数据

var person = {"name":"Andrew", "loves":"Open Source"};
var asJSON = JSON.stringify(person);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/postmethod", true);
xhttp.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhttp.send(asJSON);

【讨论】:

  • 类似的东西...
猜你喜欢
  • 2017-06-12
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多