0

我有两个文本文件正在通过 ajax 显示在网页上。一旦将更多文本添加到文本文件中,我需要更新这两个文件。完成此脚本后,我在本地主机上对其进行了测试,一切正常。现在我试图让它在我的网络主机上工作并且文本正在显示,但是当需要更新文件时,没有任何更新。
我尝试禁用 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;
?>

和第二个 PHP 文件是一样的:

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

使用此代码,txt 文件中的日志每秒都会更新和显示,而无需刷新页面。



如果有办法使用这些相同的步骤,但只有 1 个 php 文件而不是 2 个,请告诉我,我将删除我的答案并选择你的。

于 2013-07-19T05:11:34.783 回答