Ajax and 302
Did you ever wonder what happens if use request a resource via Ajax ( XmlHttpRequest or ActivexObject) for which server sends back 302 ( browser redirect ) ? Does the xmlhttp.status==302 below will ever be true ?
---------------------------------
function test302() {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==302)
{
document.getElementById("myDiv").innerHTML="resource moved."
}
}
xmlhttp.open("GET","URL",true);
xmlhttp.send();
}
------------------------
The answer is NO. Because browser handles it and never passes it to JavaScript.
---------------------------------
function test302() {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==302)
{
document.getElementById("myDiv").innerHTML="resource moved."
}
}
xmlhttp.open("GET","URL",true);
xmlhttp.send();
}
------------------------
The answer is NO. Because browser handles it and never passes it to JavaScript.
Comments
Post a Comment