Tuesday, February 26, 2008

HTTP Response 200 or 0?

var xmlhttp;

if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
xmlhttp = new XMLHttpRequest();
}

function makerequest(serverPage, objID){
var obj = document.getElementById(objID);
var url = serverPage+"?weight="+document.getElementById('weight').value;
xmlhttp.open("GET", url);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
obj.value = xmlhttp.responseText;
}
}
}

xmlhttp.send(null);
}

Look like a normal ajax request right? but do you believe that this code
won't work if you are sending the request to a local file? try me. =)

The fact is, if you are working on local files, use
xmlhttp.status == 0 instead of xmlhttp.status == 200


But my solution to put both code together so that we can connect to whatever file we want.

This is my solution.


var xmlhttp;

if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
xmlhttp = new XMLHttpRequest();
}

function makerequest(serverPage, objID){
var obj = document.getElementById(objID);
var url = serverPage+"?weight="+document.getElementById('weight').value;
xmlhttp.open("GET", url);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200 || xmlhttp.status == 0){
obj.value = xmlhttp.responseText;
}
}
}

xmlhttp.send(null);
}


0 comments: