XMLHttpRequest 不工作

发布于 2025-01-02 19:09:04 字数 820 浏览 2 评论 0原文

我使用以下代码每 2 秒动态更新 HTML 中的 TextArea 元素,而无需重新加载页面。但是,alert(request.readystate) 返回未定义而不是数字 1-4。当我尝试 if(request)alert("Request object") 时,它会按预期提醒用户“Request object”。我不知道为什么这不起作用,我已经尝试了几个小时来解决这个问题!

我的代码:

<script type="text/javascript">

    function init(){
      var url="http://www.suchandsuch.net/ChatBowl/text.txt";
      var request= new XMLHttpRequest();
      request.open("POST",url,true);
      alert(request.readystate);
      request.onload= function(){
        if (request.readystate ==4 && request.status == 200){
          document.getElementById('textarea').innerHTML=request.responseText;
        }  
      }
        request.send(null);

     }


    var int=self. setInterval('init()', 2000);

  </script> 

我感谢任何帮助。

The following code I am using to dynamically update the TextArea element in my HTML every 2 seconds without reloading the page. However, the alert(request.readystate) is returning undefined instead of a number 1-4. When I tried if(request) alert("Request object") it alerts the user "Request object" as expected. I have no clue why this isn't working and I have been trying to figure this out for hours!

My code:

<script type="text/javascript">

    function init(){
      var url="http://www.suchandsuch.net/ChatBowl/text.txt";
      var request= new XMLHttpRequest();
      request.open("POST",url,true);
      alert(request.readystate);
      request.onload= function(){
        if (request.readystate ==4 && request.status == 200){
          document.getElementById('textarea').innerHTML=request.responseText;
        }  
      }
        request.send(null);

     }


    var int=self. setInterval('init()', 2000);

  </script> 

I appreciated any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭泣的笑容 2025-01-09 19:09:04

JavaScript 区分大小写,并且您输入的 ReadyState 错误:

readyState 不是 readystate

MSDN docs

并且回调函数应该分配给 onreadystatechange:

var url="http://www.suchandsuch.net/ChatBowl/text.txt";
var request= new XMLHttpRequest();
request.onreadystatechange = function(){
    alert(request.readyState + " " + request.status);
    if (request.readyState ==4 && request.status == 200)
          document.getElementById('textarea').innerHTML=request.responseText;
    };

request.open("POST", url, true);
alert(request.readyState);  

request.send();
     

JavaScript is case sensitive and you typed the readyState wrong:

readyState not readystate

MSDN docs

And the callback function should be assigned to onreadystatechange:

var url="http://www.suchandsuch.net/ChatBowl/text.txt";
var request= new XMLHttpRequest();
request.onreadystatechange = function(){
    alert(request.readyState + " " + request.status);
    if (request.readyState ==4 && request.status == 200)
          document.getElementById('textarea').innerHTML=request.responseText;
    };

request.open("POST", url, true);
alert(request.readyState);  

request.send();      

时光倒影 2025-01-09 19:09:04

也许而不是

request.onload= function(){

尝试

request.onreadystatechange = function(){

maybe instead of

request.onload= function(){

try

request.onreadystatechange = function(){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文