Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
615 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...