简体   繁体   English

如何将Python数据发送到JavaScript

[英]How to send Python data to JavaScript

I am developing the web using google app engine. 我正在使用Google App Engine开发网络。 I used jinja2 to send data from html to main. 我使用jinja2将数据从html发送到main。 pays code. 支付代码。 The problem is how to get the data from the javascript of html. 问题是如何从html的javascript获取数据。 Please let me know the correct answer. 请让我知道正确的答案。 Thank you. 谢谢。

main.py main.py

def get(self):
    db = connect_to_cloudsql()
    cursor = db.cursor()
    cursor.execute("set names utf8")
    cursor.execute("""select no, u_name, age, gender, U_adress, phone, car_num, penalty from User;""")

    data = cursor.fetchall() 
    array_list = []

    for row in data:
        temp = (row[0], str(row[1]), row[2], str(row[3]), str(row[4]), row[5], str(row[6]), row[7])
        array_list.append(temp)

    db.close()

    data = json.dumps(array_list)
    template_values = { 'data': data }

    template = JINJA_ENVIRONMENT.get_template('User.html')
    self.response.write(template.render(template_values))

and javascript code in html. 和html中的javascript代码。

var table;

$(function() {
    $.get(function({ data }) {
        var jsonobj = JSON.parse({ data });

        table = $('#userindex').dataTable({ 
            "sPaginationType": "bootstrap",
            "sDom": "t<'row'<'col-xs-6 col-left'i><'col-xs-6 col-right'p>>",
            "bStateSave": false
        });

        $.each(jsonobj, function(key,value) {
            table.fnAddData(
                value[0],
                value[1],
                value[2],
                value[3],
                value[4],
                value[5],
                value[6],
                value[8]
            );
        });
    });
});

this is my error 这是我的错误

datatables.min.js:16 GET https://firststep-2016.appspot.com/function%20(%7Bdata%7D)%7Bvar%20jsonobj%20=%20JSON.parse(%7Bdata%7D);table%20=%20 $(' 404 () datatables.min.js:16 GET https://firststep-2016.appspot.com/function%20(%7Bdata%7D)%7Bvar%20jsonobj%20=%20JSON.parse(%7Bdata%7D);table%20 =%20 $('404()

I found PyV8 accidentally today, which I think is what you need. 我今天偶然发现了PyV8,我认为这是您所需要的。 It built a js run environment that you can run js code in python to get the text what you want. 它构建了一个js运行环境,您可以在python中运行js代码以获取所需的文本。 Here are the references. 这里是参考。

http://www.silverna.org/blog/?p=252 http://www.silverna.org/blog/?p=252

https://code.google.com/p/pyv8/ https://code.google.com/p/pyv8/

This's a good answer for sending json data to javascript with jinja. 这是使用jinja将json数据发送到javascript的好答案。 It maybe help you. 它可能会帮助您。

sending data as JSON object from Python to Javascript with Jinja 使用Jinja将数据作为JSON对象从Python发送到Javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM