2011-11-26 67 views
3

我正在使用web.py框架来设置我的网站。当我点击一个按钮时,我想将数据发送到服务器,然后通过发生器功能/收益率发回数据。基本上可以在数据准备就绪时产生数据,而不是等待我的数据功能完全结束。Web.py |通过POST和AJAX生成数据(生成器函数)

我得到yield to work via GET,但我通过AJAX的POST实现是问题。

def POST(self): 
    web.header('Content-type','text/html') 
    web.header('Transfer-Encoding','chunked') 
    yield "hello" 
    sleep(10) 
    yield "hello" 

而我的javascript:

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
     jQuery("#button").click(function() { 
      jQuery.ajax({ 
        type: "POST", 
        dataType: "text", 
        cache: false, 
        success: function(data){ 
        jQuery("#container").html(data) 
        } 
       }); }); }); 
</script> 

输出:

HelloHello (after 10 seconds) 
rather than.. 
Hello (10 second delay) Hello 

注意:我使用内置的服务器我的本地机器上web.py的。

回答

2

愚蠢的我 - AJAX不支持流媒体。所以Comet或HTML5 WebSockets会派上用场的情况就是这样。