Flask具有一个称为request的内置对象。在请求中有一个称为args的multidict。
您可以
request.args.get('key')用来检索查询字符串的值。from flask import request@app.route('/example')def example(): # here we want to get the value of the key (i.e. ?key=value) value = request.args.get('key')当然,这需要一个get请求(如果您使用post,请使用
request.form)。在javascript方面,您可以使用纯javascript或jquery进行get请求。 我将在示例中使用jquery。
$.get( url="example", data={key:value}, success=function(data) { alert('page content: ' + data); });这是将数据从客户端传递到Flask的方式。jQuery代码的功能部分是如何将数据从flask传递到jquery。举例来说,假设您有一个名为/
example的视图,并且从jquery端传入了键值对“ list_name”:“ example_name”
from flask import jsonifydef array(list): string = "" for x in list: string+= x return string@app.route("/example")def example(): list_name = request.args.get("list_name") list = get_list(list_name) #I don't know where you're getting your data from, humor me. array(list) return jsonify("list"=list)在jquery的成功函数中,你会说
success=function(data) { parsed_data = JSON.parse(data) alert('page content: ' + parsed_data); }请注意,出于安全原因,flask不允许在json响应中显示顶级列表。



