js代码:

<script>if(typeof(EventSource)!=="undefined"){var source=new EventSource("test.php");source.onmessage=function(event){ console.log(event.data)};}else{console.log('浏览器不支持Server-Sent')}</script>php代码:

<?phpheader('Content-Type: text/event-stream');$arr = array( 'name'=>'lee');$data = json_encode($arr);echo "data: {$data}\n\n";