I am using the web.py framework to set up my website. When I click a button, I want to POST data to the server and then send back data via a generator function / yield. Basically yield data as it’s ready, not wait for my data function to fully finish.
I get yield to work via GET, but my POST implementations via AJAX is the problem.
def POST(self):
web.header('Content-type','text/html')
web.header('Transfer-Encoding','chunked')
yield "hello"
sleep(10)
yield "hello"
And my 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>
Output:
HelloHello (after 10 seconds)
rather than..
Hello (10 second delay) Hello
Note: I am using web.py’s built in server on my local machine.
Foolish me – AJAX doesn’t support streaming. So this is a case when Comet or HTML5 WebSockets will come in handy.