简体   繁体   English

如何使用Tornado的self.write方法发送javascript代码段?

[英]How can I send a code snippet of javascript using Tornado's self.write method?

I was trying to send a small piece of javascript to the browser, using Tornado's self.write method: 我试图使用Tornado的self.write方法向浏览器发送一小段javascript:

@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
    [...]    #deal with arguments, db operation, and so on #async.

    #if _auth_flag is True, then send the alert
    if _auth_flag:
        self.write("<script>alert('Login Successfully');</script>")
        self.redirect("/")

    [...]    #other code

when I run the web app, however, no alert was executed. 但是,当我运行Web应用程序时,没有执行警报。

MY QUESTIONG IS: 我的问题是:

  • Why this code does not work correctly? 为什么此代码无法正常工作?

  • If the code above is not appropriate, how can I send the same alert to browser in other ways? 如果上面的代码不合适,如何以其他方式将相同的警报发送到浏览器?

If you send a redirect, the rest of the response is ignored. 如果发送重定向,则其余响应将被忽略。 You'll have to either only send a redirect and ensure that whatever JS you want appears on the following page, or only send the javascript and have the javascript assign to location.href to perform the "redirect". 您将只需要发送重定向并确保所需的JS出现在下一页上,或者仅发送javascript并将javascript分配给location.href即可执行“重定向”。

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

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