添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

EventSource's response has a MIME type ("application/x-httpd-php") that is not "text/event-stream". Aborting the connection

Ask Question

I'm trying to use EventSource and I'm using this code:

var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";

Here is my php:

header("Content-Type: text/event-stream"); header('Cache-Control: no-cache'); $time = date('r'); echo "data: The server time is: {$time}\n\n"; flush();

Above code is given in this link of w3school. But I got this error: EventSource's response has a MIME type ("application/x-httpd-php") that is not "text/event-stream". Aborting the connection.

Anyone know why? I copied everything from w3school. I can not find out what the error is?

That means PHP is sending the default mime type header first.

My first guess would be that you have some whitespace (or an invisible character, like a utf8 BOM) in your php script file before the opening <?php. (See http://php.net/manual/en/function.header.php)

Or it might be that it is sending back an error message? I'd recommend using curl, from the commandline, to debug it. (See https://stackoverflow.com/a/49486869/841830) Alternatively, using the web developer tools in your browser to poke in to exactly what is being sent back.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.