我发现了一篇非常好的文章,其中包含我想添加到页面的功能,但由于一个小错误而被困了一整天。作为参考,教程位于此处。
一切正常,唯一没有发生的事情是 index.php 网页没有刷新对托管 php 数组所做的更改。谁能看一眼我的代码并告诉我我是否有错别字或遗漏了文章的一部分?
我的数组文件 - selectedSystemStateResults.php
<?php
$selectedSystemStateResults = ["cart", "dogsss", "cows", "zebra", "snake"];
我的服务器端 PHP 脚本文件 - selectedSystemState-script.php
<?php
header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");
// Require the file which contains the $animals array
require_once "selectedSystemStateResults.php";
// Encode the php array in json format to include it in the response
$selectedSystemStateResults = json_encode($selectedSystemStateResults);
echo "data: $selectedSystemStateResults" . "\n\n";
flush();
echo "retry: 1000\n";
echo "event: selectedSystemStateResultsMessage\n";
我的客户端网页 - index.php
<?php require "selectedSystemStateResults.php"; ?>
<html>
<body>
<?php foreach ($selectedSystemStateResults as $selectedSystemStateResult) : ?>
<li><?php echo $selectedSystemStateResult; ?></li>
<?php endforeach ?>
</ul>
<script src="/selectedSystemState-script.js"></script>
</body>
</html>
我的 javascript 文件 - selectedSystemState-script.js
let eventSource = new EventSource('selectedSystemState-script.php');
eventSource.addEventListener("selectedSystemStateResultsMessage", function(event) {
let data = JSON.parse(event.data);
let listElements = document.getElementsByTagName("li");
for (let i = 0; i < listElements.length; i++) {
let selectedSystemStateResults = listElements[i].textContent;
if (!data.includes(selectedSystemStateResults)) {
listElements[i].style.color = "red";
}
}
});
在过去的 8 个小时里,我已经阅读并重新阅读了这篇文章,感觉真的很卡。有没有人看到任何刺耳的 php 或 javascript 错别字或者教程可能是错误的?
请原谅我在未经编辑的原始帖子中的文件名中的错字。该目录显示了所有正确命名的文件
海绵宝宝撒
梵蒂冈之花