responseText - XMLHttpRequest

时间:2023-05-14
本文介绍了responseText - XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我的代码中 responseText 不起作用.它应该显示,在文本框中输入的文本+:您的请求已被 syam 看到"

<html><head id="Head1" runat="server"><标题></标题><脚本类型="文本/javascript">var xmlHttpRequest;函数 sSignature(str) {xmlHttpRequest = 新的 XMLHttpRequest();xmlHttpRequest.onreadystatechange = function() {如果(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){document.getElementById("target").innerHTML = xmlHttpRequest.responseText;}}xmlHttpRequest.open("GET", "AjaxResponse.aspx?q=" + str, true);xmlHttpRequest.send();}</脚本></头><身体><form id="form1" runat="server">

输入一个字符串:<input type="text" id="textbox" onkeyup="sSignature(this.value)"/><span id="target">这里的文字应该改变</span></div></表格></身体></html>

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

string sRequest = Request.QueryString["q"];var sResponse = sRequest+ " :您的请求已被 syam 看到";Response.Write(sResponse);

解决方案

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

尝试用这个换掉它:

xmlHttpRequest.onreadystatechange = 函数(事件){var xhr = event.target;如果(xhr.readyState === 4 && xhr.status === 200){document.getElementById("target").innerHTML = xhr.responseText}};

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

解决方案

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

这篇关于responseText - XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Access-Control-Allow-Origin 和 CORS 背后的概念是什么? 下一篇:跨域进行 JavaScript 调用

相关文章