Error using ajax

I used the flush() function in an endless loop on my php page, it echoes the text every second. when I open the page in the browser, it works! but when i load it through jquery ajax it is not responding!

php page

  <?php if (ob_get_level() == 0) ob_start(); for ($i = 0; true/*$i<10*/; $i++){ echo "<br> Line to show. $i"; echo str_pad('',4096)."\n"; ob_flush(); flush(); sleep(1); } ob_end_flush(); ?> 

Jquery code

 $.ajax({ url: 'res.php', beforeSend: function( ) { $('#mydiv').html('loading...'); }, success: function( data ) { $('#mydiv').html( data ); } }); 
+2
source share
2 answers

HTTP streaming cannot be done simply with $ .get

Insert the <script> tags follows: http://ajaxpatterns.org/archive/HTTP_Streaming.php .

  <? while (true) { ?> <script type="text/javascript"> $('news').innerHTML = '<?= getLatestNews() ?>'; </script> <? flush(); // Ensure the Javascript tag is written out immediately sleep(10); } ?> 

HTTP streaming is a very complex hack. Instead, you should use a lengthy survey that works in every browser. There seem to be some solutions (slide 54)

An example of a simple polling example:

How to implement the basic "long survey"?

 This video shows how to do long-polling: http://www.screenr.com/SNH 

PS: this will surely kill your server (poor performance). You should take a look at http://pusherapp.com , which is free for small sites.

+1
source

I would say that the success handler is not called because the body of the response is never completed completely.

0
source

All Articles