在我的代码中,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);发布于 2012-01-03 17:36:43
我认为错误出在您的onreadystatechangedhandler中。它将接收一个实例参数,其中的target属性指向XHR- event。
试着把它换成这样:
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};发布于 2012-01-03 17:04:33
先发送您的请求
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;
}
}
}发布于 2012-01-03 17:56:48
请注意,您的代码将无法在Microsoft Internet Explorer中运行。
其次,修改一行代码,使其看起来更好-
xhr.send(null);的xhr.send()
https://stackoverflow.com/questions/8709733
复制相似问题