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
2 Answers
Reset to default 3JavaScript 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(){