在我的代码中,responseText不起作用。它应该显示,在文本框中输入的文本+“:你的请求已被syam看到”
<html>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var xmlHttpRequest;
function sSignature(str) {
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("target").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.open("GET", "AjaxResponse.aspx?q=" + str, true);
xmlHttpRequest.send();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
enter a string :<input type="text" id="textbox" onkeyup="sSignature(this.value)"/>
<span id="target">text should change here</span>
</div>
</form>
</body>
</html>
在代码隐藏页面中,在page_load()
中string sRequest = Request.QueryString["q"];
var sResponse = sRequest+ " :Your request has been seen by syam";
Response.Write(sResponse);
答案 0 :(得分:3)
我认为错误发生在onreadystatechangedhandler
。它将收到一个event
参数,其中target
属性指向XHR实例。
尝试用这个交换出来:
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};
答案 1 :(得分:1)
首先发送您的请求
function sSignature(str) {
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "AjaxResponse.jsp?q=" + str, true);
xmlHttpRequest.send();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("target").innerHTML = xmlHttpRequest.responseText;
}
}
}
答案 2 :(得分:-1)
请注意,您的代码无法在Microsoft Internet Explorer中使用。
其次,修改一行代码以使其看起来更好 -
xhr.send()
的 xhr.send(null);