当我将此文件加载到服务器上并从浏览器启动地址时 输入字段正在显示并且正常工作,但是当我输入新字符串时,我没有得到回复。
<!DOCTYPE html>
<html>
<body>
<h2>Sending / retrieving a message with AJAX</h2>
<label for="name">Enter message below:</label><BR>
<input type="text" id="msgID" name="name" onchange="loadDoc()" required minlength="1" size="30">
<p id="demo"></p>
<script>
var newmsg = document.getElementById("msgID").value;
var blob = new Blob([newmsg], {type: 'text/plain'});
var url = URL.createObjectURL(blob);
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
</script>
</body>
</html>
答案 0 :(得分:1)
您的网址不会在更改时更新。您应该使用msgID
中的值更新网址,并在新网址上发送XMLHttpRequest
。
<!DOCTYPE html>
<html>
<body>
<h2>Sending / retrieving a message with AJAX</h2>
<label for="name">Enter message below:</label><BR>
<input type="text" id="msgID" name="name" onchange="loadDoc()" required minlength="1" size="30">
<p id="demo"></p>
<script>
function loadDoc() {
var newmsg = document.getElementById("msgID").value;
var blob = new Blob([newmsg], {type: 'text/plain'});
var url = URL.createObjectURL(blob);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
</script>
</body>
</html>