最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHttpRequest not working - Stack Overflow

programmeradmin3浏览0评论

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=".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.

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/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.

Share Improve this question edited Feb 5, 2012 at 9:55 Tomasz Nurkiewicz 341k71 gold badges712 silver badges679 bronze badges asked Feb 5, 2012 at 9:28 moesefmoesef 4,87118 gold badges55 silver badges68 bronze badges 4
  • 1 is the url you are requesting outside of your domain? – Mikey G Commented Feb 5, 2012 at 9:30
  • Also, you should use a GET for that mand, to ply with HTML standards – Authman Apatira Commented Feb 5, 2012 at 9:35
  • you really need to make sure everything is in the right case: en.wikipedia/wiki/… – Mikey G Commented Feb 5, 2012 at 9:43
  • URL is in my own domain. Switched to GET. Still not working. – moesef Commented Feb 5, 2012 at 9:43
Add a ment  | 

2 Answers 2

Reset to default 3

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

maybe instead of

request.onload= function(){

try

request.onreadystatechange = function(){
发布评论

评论列表(0)

  1. 暂无评论