我正在编写一段代码来显示通知,但是遇到了错误。我的代码如下:
function notify() {
$response = new Symfony\Component\HttpFoundation\StreamedResponse(function() {
while (true) {
$notification = Notification::where('user_ID', '=', Auth::user()->id)->get();
echo 'data: ' . json_encode($notification) . "\n\n";
ob_flush();
flush();
sleep(3);
}
});
$response->headers->set('Content-Type', 'text/event-stream');
return $response;
}
我已经添加了以下JavaScript代码:
<script type="text/javascript">
var es = new EventSource("<?php echo action('NotificationController@notify'); ?>");
es.addEventListener("message", function(e) {
arr = JSON.parse(e.data);
//apply some effect on change, like blinking the color of modified cell...
// }
}, false);
错误信息如下:
EventSource的响应MIME类型为"text/html"而不是"text/event-stream",因此连接已中止。
我该怎么解决?我正在按照教程server-sent-events-example-laravel编写代码。