龙卷风不会将Ajax响应发送给客户端 [英] Tornado doesn't send Ajax response to client

查看:87
本文介绍了龙卷风不会将Ajax响应发送给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交表单后,Tornado服务器会进行一些检查,并将响应发送回客户端,该响应应在当前页面中显示为alert.

Upon form submit the Tornado server does some checks and sends a response back to the client, which should appear in that current page as an alert.

相反,空白HTML页面使用Json响应呈现,但在提交表单的当前页面上不显示为alert.

Instead a blank html page is rendered with the Json response, but not as an alert on the current page where the form was submitted.

提交后,表单通过post发送到/dh (DataHandler)

On submit the form is sent via post to /dh (DataHandler)

这是Jquery:

$.post("/dh",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
    },"json");

龙卷风代码:

class DataHandler(BaseHandler): 
    def post(self):

        # Checks are done with form data received

        dupInfo={
            'tel' : duptel,
            'name' : dupName
             }

        self.write(json.dumps(dupInfo, default=json_util.default))
        self.finish()

那么如何将这个json返回到当前页面?

So how can you return this json to the current page?

推荐答案

在"alert"语句之后,添加return false;.这将禁用浏览器对POST事件的默认处理.浏览器的默认行为是导航到新URL,并且您想防止这种情况.

After the "alert" statement, add return false;. This disables the browser's default handling of the POST event. The browser's default behavior is to navigate to the new URL, and you want to prevent that.

这篇关于龙卷风不会将Ajax响应发送给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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