Javascript 使用Ajax更新页面上的数据,该页面使用PHP从文本文件读取
我想使用Javascript 使用Ajax更新页面上的数据,该页面使用PHP从文本文件读取,javascript,php,ajax,Javascript,Php,Ajax,我想使用Ajax更新网页上的数据。使用PHP脚本读取此数据 我希望这个功能每5秒执行一次。出于某种原因,这根本没有发生 我的JavaScript: <script> function refresh(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 20
Ajax
更新网页上的数据。使用PHP
脚本读取此数据
我希望这个功能每5秒执行一次。出于某种原因,这根本没有发生
我的JavaScript:
<script>
function refresh(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementClassName("scroll").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gm.php?q=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>", true);
// The PHP variables refer to a specific file
xmlhttp.send();
}
setInterval(refresh,5000);
</script>
<script>
function refresh(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementsByClassName("scroll")[1].innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gm.php?q=" + '<?php echo $mf[0]."-".$mf[1].".txt" ?>', true);
// The PHP variables refer to a specific file
xmlhttp.send();
}
setInterval(refresh,5000);
</script>
我发现我的元素选择器是错误的,因为我使用的是getelementclassname
而不是getElementsByClassName
。另外,我没有在类中给出元素索引,因此无法选择元素
另一个原因是我在包含PHP脚本的JavaScript字符串中使用了引号
我的JavaScript:
<script>
function refresh(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementClassName("scroll").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gm.php?q=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>", true);
// The PHP variables refer to a specific file
xmlhttp.send();
}
setInterval(refresh,5000);
</script>
<script>
function refresh(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementsByClassName("scroll")[1].innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gm.php?q=" + '<?php echo $mf[0]."-".$mf[1].".txt" ?>', true);
// The PHP variables refer to a specific file
xmlhttp.send();
}
setInterval(refresh,5000);
</script>
document.getElementsClassName
NOTdocument.getElementClassName
检查url参数Quotes我已将其更正为document.getElementsByClassName(“滚动”)
,仍然没有帮助请打开浏览器中的控制台读取错误,然后可以修复它。该函数每5秒运行一次,这不是问题所在。刷新函数中有一些错误。查看浏览器控制台以测试这一点。可能是CORS错误或传递无效参数