2013-07-14 76 views
0

我有两个文本文件通过ajax显示在网页上。只要将更多文本添加到文本文件中,我就需要更新这两个文件。当我完成这个脚本时,我在本地主机上测试了它,并且所有工作都正常。现在我试图让它在我的虚拟主机上运行,​​文本正在显示,但是当更新文件时没有更新。
我试过禁用ajax响应的缓存,但文件仍然不更新。Ajax和setInterval()自动更新文本文件没有更新

下面是代码:

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



有什么需要在服务器上进行更改或者这是一个问题,我的代码?
我在做什么错?

回答

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> 

这里是用来diplay文件的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的方式告诉我,我会删除我的答案和选择你的。