【发布时间】:2013-09-08 22:36:18
【问题描述】:
我的 HTML 文件中的代码:
<!doctype html>
<head>
<title>Notes</title>
<script>
function PullNotes() {
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "notes.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4 || txtFile.status == 200) {
allText = txtFile.responseText;
}
document.getElementById('notetext').innerHTML = allText;
}
}
</script>
</head>
<body>
<div id="notetext"><p>Failed.</p></div>
<input type="button" value="Pull Notes" onClick="PullNotes()">
</body>
</html>
当我单击按钮时。什么都没发生。
如果您想知道为什么它显示“失败”,那么我知道 JavaScript 没有更新。
谢谢, ~BN
【问题讨论】:
-
notes.txt 是在您的文件系统上还是在 http 服务器上?如果您通过foo.com/index.html 访问此html 页面,则notes.txt 需要为@foo.com/notes.txt。你还需要
send()我认为的请求。 -
把它放在你的 if 块中:
document.getElementById('notetext').innerHTML = allText; -
@SB。服务器。我在本地和服务器上都试过了。
标签: javascript html button onclick xmlhttprequest