0

ajax を介して Web ページに表示されている 2 つのテキスト ファイルがあります。テキスト ファイルにテキストが追加されたらすぐに、これら 2 つのファイルを更新する必要があります。このスクリプトを完成させて、自分のローカルホストでテストしたところ、すべて正常に動作していました。今、私はそれを自分のウェブホストで動作させようとしましたが、テキストが表示されていますが、ファイルを更新するときに何も更新されません。
ajax 応答のキャッシュを無効にしようとしましたが、ファイルはまだ更新されません。

コードは次のとおりです。

<html>
<head>
<script>
$.ajaxSetup ({
cache: false
});

  function UpdateDAU()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           document.getElementById("UpdateD").innerHTML=xmlhttp.responseText.split('\n').join('<br/>';
        }
      }
    xmlhttp.open("GET","../logs/dau.txt",true);
    xmlhttp.send();
    }

    function UpdateFireBox()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("UpdateF").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
        }
      }
    xmlhttp.open("GET","../logs/firebox.txt",true);
    xmlhttp.send();
    }


    </script>
</head>
<body>
    <script type="text/javascript">
    UpdateDAU();
    </script>
    <div id="UpdateD">No Logs</div>
    <script type="text/javascript">
    setInterval("UpdateDAU", 1000);
    </script>

    <script type="text/javascript">
    UpdateFireBox();
   </script>
<div id="UpdateF">No Logs</div>
    <script>
    setInterval("UpdateFireBox", 1000);
    </script>

</body>
</html>



サーバー上で変更する必要があるものはありますか、それとも私のコードの問題ですか?
私は何を間違っていますか?

4

1 に答える 1

0

先週の調査の後、ようやくディスプレイが適切に更新されました。テキストファイルから更新するファイルを取得できなかったため、ajax が表示する前に、php ファイルからファイルをエコーする必要がありました。
これは複数のマシンで動作しているコードです:

<html>
<head>
<script>
$.ajaxSetup ({
// Disable caching of AJAX responses */
cache: false
});

  function UpdateDAU()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("UpdateD").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
        }
      }
    xmlhttp.open("GET","getdau.php",true);
    xmlhttp.send();
    setTimeout(function() {
    UpdateDAU();       
    }, 1000)
    }

    function UpdateFireBox()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("UpdateF").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
        }
      }
    xmlhttp.open("GET","getfirebox.php",true);
    xmlhttp.send();
    setTimeout(function() {
    UpdateFireBox();       
    }, 1000)
    }


    </script>
</head>
<body>
    <script type="text/javascript">
    UpdateDAU();
</script>
<div id="UpdateD"><p class="text-warning">If there is no page...<br> Then the guards have not started there rounds.</p></div>

    <script type="text/javascript">
    UpdateFireBox();
</script>
<div id="UpdateF"><p class="text-warning">If there is no page...<br> Then the guards have not started there rounds.</p></div>

</body>
</html>

ファイルの表示に使用される PHP ファイルは次のとおりです。

<?php
$file = file_get_contents('../logs/dau.txt', true);
echo $file;
?>

2 番目の PHP ファイルは同じです。

<?php
$file = file_get_contents('../logs/firebox.txt', true);
echo $file;
?>

このコードを使用すると、txt ファイル内のログが更新され、ページを更新することなく毎秒表示されます。



これらの同じ手順を使用する方法があるが、2 つではなく 1 つの php ファイルのみを使用する方法がある場合は、回答を削除してあなたの回答を選択します。

于 2013-07-19T05:11:34.783 に答える