第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

服務(wù)器端事件、HTML5、PHP 和 Javascript...索引頁(yè)不“刷新”

服務(wù)器端事件、HTML5、PHP 和 Javascript...索引頁(yè)不“刷新”

PHP
冉冉說(shuō) 2021-09-18 13:54:33
我發(fā)現(xiàn)了一篇非常好的文章,其中包含我想添加到頁(yè)面的功能,但由于一個(gè)小錯(cuò)誤而被困了一整天。作為參考,教程位于此處。一切正常,唯一沒(méi)有發(fā)生的事情是 index.php 網(wǎng)頁(yè)沒(méi)有刷新對(duì)托管 php 數(shù)組所做的更改。誰(shuí)能看一眼我的代碼并告訴我我是否有錯(cuò)別字或遺漏了文章的一部分?我的數(shù)組文件 - selectedSystemStateResults.php<?php$selectedSystemStateResults = ["cart", "dogsss", "cows", "zebra", "snake"];我的服務(wù)器端 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";我的客戶端網(wǎng)頁(yè) - 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";    }  }});在過(guò)去的 8 個(gè)小時(shí)里,我已經(jīng)閱讀并重新閱讀了這篇文章,感覺(jué)真的很卡。有沒(méi)有人看到任何刺耳的 php 或 javascript 錯(cuò)別字或者教程可能是錯(cuò)誤的?請(qǐng)?jiān)徫以谖唇?jīng)編輯的原始帖子中的文件名中的錯(cuò)字。該目錄顯示了所有正確命名的文件
查看完整描述

2 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

與您的 javascript 文件名不匹配selectSystemState-script.js。下次通過(guò)打開(kāi)開(kāi)發(fā)者工具控制臺(tái)來(lái)驗(yàn)證 javascript 錯(cuò)誤!


另一個(gè)錯(cuò)誤是您在設(shè)置事件名稱(chēng)之前發(fā)送數(shù)據(jù)。結(jié)尾selectedSystemState-script.php應(yīng)該是:


echo "retry: 1000\n";

echo "event: selectedSystemStateResultsMessage\n";

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

flush();


查看完整回答
反對(duì) 回復(fù) 2021-09-18
?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

使用本教程使用服務(wù)器發(fā)送的事件


我發(fā)現(xiàn) script.php 文件不能停止執(zhí)行?。?/p>


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


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


嘗試這個(gè)


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

這對(duì)我來(lái)說(shuō)是新的,但我注意到了一些事情:


1- php 事件腳本文件必須有標(biāo)題 text/event-stream


2- 該文件不能停止執(zhí)行!


3-event:之前發(fā)送data:。


希望這有幫助


編輯 在對(duì)你的腳本進(jìn)行測(cè)試之后它在我改變時(shí)起作用了 <script src="/selectedSystemState-script.js"></script>


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


它是selectedSystemState-script.js從根文件夾調(diào)用的!并產(chǎn)生 404 錯(cuò)誤


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

    }

  }

});


查看完整回答
反對(duì) 回復(fù) 2021-09-18
  • 2 回答
  • 0 關(guān)注
  • 181 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)