将JavaScript数组传递给Flask [英] Passing Javascript Array to Flask

查看:357
本文介绍了将JavaScript数组传递给Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  def我在烧瓶中有一个名为array的函数,它接受一个列表并打印列表中的项目。数组(列表):
string =
列表中的x:
string + = x
返回字符串

在客户端,我想传入一个名为str的javascript数组到这个数组中。我该怎么做?这是我现在所拥有的,但是Flask没有读取添加的变量。任何想法?
$ b $ pre $ for(var i = 0; i< response.data.length; i ++){
的console.log(ⅰ);
$ b $ // str = str +< br />< b> Pic< / b>:< img src ='+ response.data [i] .picture +' />中;

str [i] = response.data [i] .picture;
}
window.location ={{url_for('array',str = list)}};


解决方案

Flask有一个名为request的内置对象。请求中有一个叫做args的multidict。



您可以使用 request.args.get('key')来检索查询字符串的值。

  from flask import request 

@ app.route('/ example')
def example():
#在这里我们想要获得键值(ie?key = value)
value = request.args。 get('key')

当然这需要一个get请求(如果你使用了一个post, 的request.form )。 在JavaScript方面,您可以使用纯JavaScript或jquery进行获取请求。
我打算在我的例子中使用jquery。
$ b $ $ $ $ $ $ $ $ $ get(
url =example ,
data = {key:value},
success = function(data){
alert('page content:'+ data);
}
);

这是如何将客户端的数据传递给瓶子的。 jquery代码的功能部分是如何将数据从瓶子传递给jquery。比方说,例如你有一个名为/ example的视图,并从jquery一侧传入一个list_name键值对:example_name

  from flask import jsonify 
def array(list):
string =
列表中的x:
string + = x
返回字符串

@ app.route(/ example)
def example():
list_name = request.args.get(list_name)
list = get_list list_name)#我不知道你从哪里得到你的数据,幽默我。
array(list)
return jsonify(list= list)

并在jQuery中的成功函数,你会说
$ b $ pre code成功=函数(数据){
parsed_data = JSON。解析(数据)
alert('page content:'+ parsed_data);
}

注意,由于安全原因,flask不允许在json响应中使用顶级列表。


I have a function in flask called array that takes in a list and prints out the items in the list:

def array(list):
    string = ""
    for x in list:
        string+= x
    return string

On the client side, I want to pass in a javascript array named str into this array. How would I do that? Here's what I have right now, but Flask isn't reading the added variable. Any ideas?

for (var i = 0; i < response.data.length; i++) {
            console.log(i);

            // str = str + "<br/><b>Pic</b> : <img src='"+ response.data[i].picture +"'/>";

            str[i] = response.data[i].picture;
        }
        window.location = "{{ url_for('array', str=list ) }}";

解决方案

Flask has a built in object called request. In request there is a multidict called args.

You can use request.args.get('key') to retrieve the value of a query string.

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')

Of course this requires a get request (if you use a post then use request.form). On the javascript side you can make a get request using pure javascript or jquery. I'm going to use jquery in my example.

$.get(
    url="example",
    data={key:value}, 
    success=function(data) {
       alert('page content: ' + data);
    }
);

This is how you pass data from the client into flask. The function portion of the jquery code is how you pass data from flask to jquery. Let's say for example you have a view called /example and from the jquery side you pass in a key value pair of "list_name":"example_name"

from flask import jsonify
def 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) 

and in the success function in jquery you'd say

  success=function(data) {
       parsed_data = JSON.parse(data)
       alert('page content: ' + parsed_data);
    }

Note that flask does not allow for top level lists in json response for security reasons.

这篇关于将JavaScript数组传递给Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆