Accessing state property of xmlhttprequest before readyState 4 in IE

Like IllegalStateException in java, IE also throws Undefined Error when you try to access xmlhttprequest's status property before reaching steady state 4.

For example in the following code

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var status = xmlhttp.status;
var isSuccess = status >= 200 && status < 300 || status === 1223 || status === 304;
if (xmlhttp.readyState==4 && isSuccess)
{
alert(xmlhttp.responseText);
}
}

alert("in ajax");

xmlhttp.open("GET","test.html");
xmlhttp.send();

as xmlhttp property status is being accessed before checking for readyState as 4, an error will be thrown in IE, works fine in firefox and chrome

Comments

navya said…
Wow. This really made my day. Thanks a lot!

JavaScript Training in CHennai JavaScript Training in CHennai JQuery Online Training JQuery Online Training
HTML5 CSS3 JavaScript Training in Chennai | HTML5 Online Training
navya said…
Wow. This really made my day. Thanks a lot!

JavaScript Training in CHennai HTML5 Training in Chennai HTML5 Training in Chennai JQuery Training in Chennai JQuery Training in Chennai JavaScript Training in Chennai JavaScript Training in Chennai

navya said…
Wow. This really made my day. Thanks a lot!

JavaScript Training in CHennai JavaScript Training in CHennai JQuery Online Training JQuery Online Training
HTML5 CSS3 JavaScript Training in Chennai | HTML5 Online Training

Popular posts from this blog

Proper way to have an anchor tag with onclick event

My FIRE Journey

CORS issues with IE9 and workarounds