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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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