I'm currently working on a project where I need to update the TextArea element in my HTML every 2 seconds dynamically, without reloading the page. The issue I'm facing is that when I use alert(request.readystate)
, it returns undefined instead of a number between 1 and 4. However, when I tried
if(request) alert("Request object")
, it correctly alerts "Request object". I've been stuck on this problem for hours and can't seem to figure out why it's not working!
Here's 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 would greatly appreciate any help.