简体   繁体   English

如何引用瓶子等Web框架中的javascript返回的数据?

[英]How to reference returned data in javascript from a web framework like bottle?

I want to query my database, and return the results to javascript. 我想查询我的数据库,并将结果返回给javascript。

javascript files that I'm serving using bottle 我使用瓶子提供的javascript文件

    @app.route('/static/<filename:path>', name='static')
    def send_static(filename):
        return static_file(filename, root='static')

and I'm not sure if I'm doing this correctly, but I added another route to specify which js file should get it. 并且我不确定我是否正确执行此操作,但是我添加了另一条路由来指定应由哪个js文件获取它。

    @app.route('/static/js/app.js')
    def ello():
        cur.execute("SELECT * FROM places")
        places= cur.fetchall()
        return {'places':places}

and then in that app.js file, I want to be able to do something like: 然后在该app.js文件中,我希望能够执行以下操作:

    console.log(places);

But I'm not really sure how to escape it. 但是我不确定如何逃避它。 I'm using mako for the html templating which works fine but everything I've tried for javascript results in 我正在将mako用于html模板,效果很好,但是我尝试过的javascript结果都在

    Uncaught SyntaxError: Unexpected token {

as suggested here: https://stackoverflow.com/a/14157401/1543640 如此处建议的那样: https : //stackoverflow.com/a/14157401/1543640

As it looks to me, you are trying to create a JavaScript file through Python. 在我看来,您正在尝试通过Python 创建一个JavaScript文件。 That sounds like a very awkward and inconvenient way to go about exchanging data between two worlds. 听起来这在两个世界之间交换数据非常尴尬且不方便。

A common practice to communicate between the front-end (JavaScript) and the back-end (a Python app in your case) is through JSON (which, in turn, is JavaScript). 在前端(JavaScript)和后端(在您的情况下为Python应用程序)之间进行通信的一种常见做法是通过JSON (反过来就是 JavaScript)。 On a very basic level, you let your Python app construct valid JSON and make it available on a specific route for your JavaScript to consume. 在最基本的层次上,您可以让Python应用程序构造有效的JSON,并使其在特定路径上可用,以供JavaScript使用。

An example would be: 一个例子是:

@app.route('/some/route/to/consume')
def fetch():
    """
    Fetch all the fancy stuff from the database.
    """
    db = Database()
    # Fetch all records, convert them into JSON
    result = json.dumps(db.get_stuff())
    return result

This function will fetch all records from the database (with a fictional function db.get_stuff() ) and, convert the result to valid JSON and return it. 此函数将从数据库中获取所有记录(使用虚拟函数db.get_stuff() ),然后将结果转换为有效JSON并返回。 The important bit is the json_dumps() function from the Python json module. 重要的一点是Python json模块中的json_dumps()函数。 It will convert valid Python data structures into valid JSON data structures. 它将有效的Python数据结构转换为有效的JSON数据结构。

Next you should write your actual JavaScript file to consume that JSON and let it do whatever you want it to do. 接下来,您应该编写您的实际JavaScript文件以使用该JSON,并让它做您想做的任何事情。 This can be done via an XHR call (AJAX), for example. 例如,这可以通过XHR调用(AJAX)完成。

If the data that will be exposed on your route is not public, do take common security practices in mind and protect the route from unwanted eyes. 如果将在您的路线上公开的数据不是公开的,请记住通用的安全做法,并保护该路线以免受到不必要的注意。

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

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