[英]Sending message from HTML input box using ajax and showing response
When i load this file on my server and launch the address from my browser the input field is showing and working but when I enter a new string I dont get a response back. 当我将此文件加载到服务器上并从浏览器启动地址时,输入字段将显示并正常工作,但是当我输入新字符串时,我没有得到回复。
<!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>
Your url is not updated onchange. 您的网址不会按onchange更新。 You should update the url with the value from msgID
and send XMLHttpRequest
on the new url. 您应该使用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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.