我第一次尝试使用服务器发送事件。我正在创建一个非常简单的测试:
// sse-test.php
<?php
header("Content-type: text/event-stream");
header("Cache-Control: no-store");
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
// inside index.html
...
const source = new EventSource("sse_test.php");
// check headers
fetch('/sse-test.php')
.then(res => {
for (let value of res.headers.values()) {
console.log(value)
}
})
EventSource构造函数抛出了一个EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection
错误。我正在记录响应头,它们似乎是正确的,它们包括一个text/event-stream;
头。
我错过了什么?
1条答案
按热度按时间mbyulnm01#
您不需要向事件源服务器发送 AJAX 请求-您可以像上面那样使用
new EventSource
进行连接,但随后将事件处理程序绑定到返回的event-stream
一个完整的基本示例可能会有所帮助。