admin管理员组文章数量:1290961
in my code responseText is not working. It is supposed to display, text entered in the text box +" :Your request has been seen by 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>
In the code-behind page, in page_load()
string sRequest = Request.QueryString["q"];
var sResponse = sRequest+ " :Your request has been seen by syam";
Response.Write(sResponse);
in my code responseText is not working. It is supposed to display, text entered in the text box +" :Your request has been seen by 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>
In the code-behind page, in page_load()
string sRequest = Request.QueryString["q"];
var sResponse = sRequest+ " :Your request has been seen by syam";
Response.Write(sResponse);
Share
Improve this question
edited Jan 3, 2012 at 9:04
nikc
17k6 gold badges52 silver badges83 bronze badges
asked Jan 3, 2012 at 8:43
dotnetrocksdotnetrocks
2,72712 gold badges38 silver badges56 bronze badges
5
- 1 Can you do better than "Not working"? What does the Net traffic inspector say? Are there errors in the console? Did you try to see how far in the code it goes before it bugs out? – Amadan Commented Jan 3, 2012 at 8:50
- it is giving an error in the line when readystate is 4 document.getElementById("target").innerHTML = xmlHttpRequest.responseText; – dotnetrocks Commented Jan 3, 2012 at 9:03
- check your code-behind page, is there you getting query string successfully – Dau Commented Jan 3, 2012 at 9:05
- You should get the responsetText from a callback only..I think so – Kris Commented Jan 3, 2012 at 9:23
-
This worked for me with PHP back-end.
$sRequest = $_GET["q"]; $sResponse = $sRequest . " :Your request has been seen by syam"; echo $sResponse;
. So please check your ASP code. – Diode Commented Jan 3, 2012 at 10:21
3 Answers
Reset to default 3I believe the error is in your onreadystatechangedhandler
. It will receive an event
param, in which the target
property points to the XHR-instance.
Try swapping it out with this:
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};
send your request first
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;
}
}
}
Mind that your code will not work in Microsoft Internet Explorer.
Secondly, modify one line of code to make it look better -
xhr.send()
by xhr.send(null);
本文标签: javascriptresponseTextXMLHttpRequestStack Overflow
版权声明:本文标题:javascript - responseText - XMLHttpRequest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520853a2383174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论