I ve been trying to figure out how to make this work.
var request;
if(window.XMLHttpRequest){
request= new XMLHttpRequest();
}else{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
var handleStateChange = function () {
switch (request.readyState) {
case 0 : // UNINITIALIZED
case 1 : // LOADING
case 2 : // LOADED
case 3 : // INTERACTIVE
break;
case 4 : // COMPLETED
break;
default: alert("error");
}
}
/*request.onreadystatechange=handleStateChange;*/
request.onreadystatechange = function(){
if((request.status === 200) && (request.readyState === 4)){
console.log(request);
document.writeln(request.responseText);
}
}
request.open('GET','data.txt');
request.send();
I found similar problems here in stackoverflow, but yet I havent figured out why its behaving this way (Im new to Ajax).
So the problem is, when I have request.open('GET','data.txt');
its causing the page to stay on loading mode and the console.log doesnt show anything.
I google around and found in stackoverflow this solution request.onreadystatechange=handleStateChange;
which seems to be fixing the problem. unfortunately it overrides the request.onreadystatechange = function(){}
. Console works and the data.txt content wont show on the web.
If I comment it out, the content shows on the web, but the page keeps loading again.
I figured that its something to do with readyState and that by the time it reaches 4 the responseText is empty. But how can I get it to show the content and the console.log?
Thank you in advance for your time :)