Javascript responseText - XMLHttpRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8709733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:10:44  来源:igfitidea点击:

responseText - XMLHttpRequest

javascriptxmlhttprequest

提问by dotnetrocks

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"

在我的代码 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> 

In the code-behind page, in page_load()

在代码隐藏页面中,在 page_load()

string sRequest = Request.QueryString["q"];
var sResponse = sRequest+ " :Your request has been seen by syam";
Response.Write(sResponse);

回答by nikc.org

I believe the error is in your onreadystatechangedhandler. It will receive an eventparam, in which the targetproperty points to the XHR-instance.

我相信错误出在您的onreadystatechangedhandler. 它将接收一个event参数,其中target属性指向 XHR 实例。

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

回答by run

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

}

回答by Acn

Mind that your code will not work in Microsoft Internet Explorer.

请注意,您的代码将无法在 Microsoft Internet Explorer 中运行。

Secondly, modify one line of code to make it look better -

其次,修改一行代码,让它看起来更好——

xhr.send()by xhr.send(null);

xhr.send()经过 xhr.send(null);