亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

服務器端事件、HTML5、PHP 和 Javascript...索引頁不“刷新”

服務器端事件、HTML5、PHP 和 Javascript...索引頁不“刷新”

PHP
冉冉說 2021-09-18 13:54:33
我發現了一篇非常好的文章,其中包含我想添加到頁面的功能,但由于一個小錯誤而被困了一整天。作為參考,教程位于此處。一切正常,唯一沒有發生的事情是 index.php 網頁沒有刷新對托管 php 數組所做的更改。誰能看一眼我的代碼并告訴我我是否有錯別字或遺漏了文章的一部分?我的數組文件 - selectedSystemStateResults.php<?php$selectedSystemStateResults = ["cart", "dogsss", "cows", "zebra", "snake"];我的服務器端 PHP 腳本文件 - selectedSystemState-script.php<?phpheader("Cache-Control: no-cache");header("Content-Type: text/event-stream");// Require the file which contains the $animals arrayrequire_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.jslet 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 錯別字或者教程可能是錯誤的?請原諒我在未經編輯的原始帖子中的文件名中的錯字。該目錄顯示了所有正確命名的文件
查看完整描述

2 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

<script src="/selectedSystemState-script.js"></script>

與您的 javascript 文件名不匹配selectSystemState-script.js。下次通過打開開發者工具控制臺來驗證 javascript 錯誤!


另一個錯誤是您在設置事件名稱之前發送數據。結尾selectedSystemState-script.php應該是:


echo "retry: 1000\n";

echo "event: selectedSystemStateResultsMessage\n";

echo "data: $selectedSystemStateResults" . "\n\n";

flush();


查看完整回答
反對 回復 2021-09-18
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

使用本教程使用服務器發送的事件


我發現 script.php 文件不能停止執行?。?/p>


或 (selectedSystemState-script.php) 在您的情況下。


所以我猜你鏈接的教程在某些方面是錯誤的?


嘗試這個


while (1) {

  // Every second, send a "selectedSystemStateResultsMessage" event.


  echo "event: selectedSystemStateResultsMessage\n";

  require("selectedSystemStateResults.php");

  $selectedSystemStateResults = json_encode($selectedSystemStateResults);

  echo "data: $selectedSystemStateResults" . "\n\n";

  ob_end_flush();

  flush();

  sleep(1);

這對我來說是新的,但我注意到了一些事情:


1- php 事件腳本文件必須有標題 text/event-stream


2- 該文件不能停止執行!


3-event:之前發送data:。


希望這有幫助


編輯 在對你的腳本進行測試之后它在我改變時起作用了 <script src="/selectedSystemState-script.js"></script>


到 <script src="./selectedSystemState-script.js"></script>


它是selectedSystemState-script.js從根文件夾調用的!并產生 404 錯誤


并在 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);


// data after event

flush();

echo "retry: 1000\n";

echo "event: selectedSystemStateResultsMessage\n";

echo "data: $selectedSystemStateResults" . "\n\n";

?>

我編輯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";

    } else {

        listElements[i].style.color = "blue";

    }

  }

});


查看完整回答
反對 回復 2021-09-18
  • 2 回答
  • 0 關注
  • 183 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號