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
Ask Question
Hi I get This error in my console using sse
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
js code is :
if (typeof(EventSource) !== "undefined")
var source = new EventSource("../api/updateWellData.php?uid=<?php echo $node_id ?>");
source.onmessage = function(event) {
var response = JSON.parse(event.data);
document.getElementById("result").innerHTML = response.test;
// some code like the above line
// refresh the page every 30 secs
The PHP Code is :
header('Cache-Control: no-cache');
header("Access-Control-Allow-Origin: *");
header("Content-Type: text/event-stream");
require_once("../resources/config.php");
if (isset($_GET['uid']))
$uid = $_GET['uid'];
while (1)
$query = Query("SELECT * FROM well_data_last WHERE well_detail_id = $uid");
$result = fetch_array($query);
echo json_encode($result);
ob_end_flush();
flush();
sleep(1);
This is my first time using sse I used the following documents : mozilla | w3schools
–
This error occurs when your PHP code outputs text in the wrong format.
This may be either a PHP error message (all of which are outputted as raw HTML), or some other text on the page which does not fit the text/event-stream
format.
If the output of the PHP file does not match the format required by text/event-stream
, it will fall back to using text/html
. Javascript requires event streams use the text/event-stream
content type so the JS console will show an error, but this is just a symptom of the actual problem - to fix the issue you need to fix your PHP.
In OP's case, the issue is with their echo
statement. All data outputted to the stream must be proceeded by data:
, and ended with a newline \n
. The stream itself must end with another newline to indicate that no more data:
messages follow.
To fix the error, OP should change
echo json_encode($result);
echo "data: " . json_encode($result) . "\n\n";
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.