[英]Can't get AJAX script to work
I have a table which has a list of transactions and am trying to do update the table contents at set intervals. 我有一个包含事务列表的表,正在尝试按设置的时间间隔更新表内容。 I am running this page on a linux red hat server.
我正在linux red hat服务器上运行此页面。 It is just the AJAX that is not working right now.
只是AJAX目前无法正常工作。
<!doctype html>
<html>
<head>
<script>
function updateTrans() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.HTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("transactions").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "update_trans.txt", true);
xmlhttp.send();
}
window.setInterval(updateTrans(), 4000);
</script>
<link href="trans_styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 id="heading"> Chomp The Bit </h1>
<div id="transactions">
<table id="trans_tbl" border="0">
<tr>
<th colspan="2">Latest Transactions</th>
</tr>
<tr>
<td>aaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td>
<td>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td>
</tr>
<tr>
<td>bbbbbbbbbbbbbbbbbbbbbbbbbbbbb</td>
<td>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</td>
</tr>
<tr>
<td>ccccccccccccccccccccccccccccc</td>
<td>cccccccccccccccccccccccccccccc</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddd</td>
<td>dddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>eeeeeeeeeeeeeeeeeeeeeeeeeeeee</td>
<td>eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee</td>
</tr>
<tr>
<td>fffffffffffffffffffffffffffff</td>
<td>ffffffffffffffffffffffffffffffff</td>
</tr>
<tr>
<td>ggggggggggggggggggggggggggggg</td>
<td>gggggggggggggggggggggggggggggggg</td>
</tr>
<tr>
<td>hhhhhhhhhhhhhhhhhhhhhhhhhhhhh</td>
<td>hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</td>
</tr>
<tr>
<td>iiiiiiiiiiiiiiiiiiiiiiiiiiiiii</td>
<td>iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii</td>
</tr>
<tr>
<td>jjjjjjjjjjjjjjjjjjjjjjjjjjjjjj</td>
<td>jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj</td>
</tr>
</table>
</div>
</body>
</html>
Make sure to have, update_trans.txt
file in the same directory, and it is not empty. 确保在同一目录中具有
update_trans.txt
文件,并且该文件不为空。
<head>
<script>
function updateTrans() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.HTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("transactions").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "update_trans.txt", true);
xmlhttp.send();
}
window.setInterval(updateTrans(), 4000);
</script>
<link href="trans_styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 id="heading"> Chomp The Bit </h1>
<div id="transactions">
</div>
</body>
</html>
I'm not sure whether this will fix the issue, but you should do window.setInterval(updateTrans, 4000)
rather than window.setInterval(updateTrans(), 4000)
. 我不确定这是否可以解决问题,但您应该执行
window.setInterval(updateTrans, 4000)
而不是window.setInterval(updateTrans(), 4000)
。 That is, you should pass a reference to the function as the first argument to setInterval
, rather than the return value of calling the function, which is what you're doing in your current code. 也就是说,您应该将对函数的引用作为
setInterval
的第一个参数传递,而不是调用函数的返回值,这是您在当前代码中所做的事情。 In your current code, updateTrans
will get called one time -- when you call it yourself -- but won't get called every 4000ms, as you seem to intend. 在您当前的代码中,
updateTrans
将被调用一次(当您自己调用时),但是不会像预期的那样每4000毫秒调用一次。
I'd also recommend that you use setTimeout
instead of setInterval
-- I wrote a post about this . 我还建议您使用
setTimeout
而不是setInterval
我写了一篇关于此的文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.