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

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

javascript - responseText - XMLHttpRequest

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...