簡體   English   中英

(龍卷風)如何在按下按鈕時將參數傳遞給服務器?

[英](tornado)how can I pass argument to server when a button is pressed?

我是python龍卷風的新手。我現在正在構建一個網站。當我想通過按按鈕向服務器發送參數時,我不知道如何在龍卷風上捕獲它。 我如何知道按下了哪個按鈕?

一個簡單的帶有jquery的ajax GET請求可以完成此工作:

class Application(tornado.web.Application):
    """Tornado web class. Create all the routes used by tornado_start"""

    def __init__(self):
        handlers = [
            (r"/", Index),
            (r"/explicit_action_url/", ActionHandler)
        ]
...

class ActionHandler(tornado.web.RequestHandler):
    def get(self):
        print("button click")


class Index(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html")

並在您的index.html中

<button id="btn" type="button">click me</button>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $("#btn").click(function () {
        $.ajax({
            type: 'GET',
            url: "/explicit_action_url/",
            success: function (data) {
                alert("success")
            }
        });
    });
</script>

您需要創建一個websocket處理程序類,該類從tornado.websocket.WebSocketHandler子類化),並在您的處理程序類中重寫on_message方法。

http://www.tornadoweb.org/en/branch2.4/websocket.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM