Thursday, October 09, 2008

Google Chrome XMLHttpRequest Behaviour

This little thing had bug me for sometimes and I managed to hunt it down for the past 1 hour.

Apparently, my brilliant AJAX application works perfectly on IE and Firefox, and by surprise, it worked on Opera and Safari as well. But... not on Google's Chrome!

With disappointment and partly also due to the lack of ability for me to debug the javascript codes on Chrome, I've leave it for sometimes.

And finally, it got fixed today!

In a nut shell, this is the finding:
var req = new XMLHttpRequest();
.... setting of url etc ...
var tagValue = "";
req.setRequestHeader("sometag", tagValue);


Basically, when you did this and execute the send function, you'll end up getting a status code of 0. And then, you'll start scratching your head because you won't find the http request in your web server as well.

Workaround?... simply set something to it, for me, I've put it as javascript null, and it simply translate it to string "null".

Hope this will help developers that read this post.

regards,
choongseng

5 comments:

Adhir Dutta said...

var xmlhttp

function GetXmlHttpObject()
{
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if(window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
//alert("No Ajax object");
return null;
}

function processData()
{
if (xmlhttp.readyState==4)
{
alert("I am here xmlhttp.onreadystatechange");

if(xmlhttp.status==200)
{
//alert(xmlhttp.status);
//alert(xmlhttp.responseText);

var putTxt=document.getElementById("Tme");
putTxt.innerHTML=xmlhttp.responseText;

//document.loginForm.Time.value=xmlhttp.responseText;
//alert(xmlhttp.statusText);
alert(putTxt.innerHTML);
}
else{alert("Problem retrieving data:" + xmlhttp.status);}
}
}


function LogIN()
{
xmlhttp=null;
xmlhttp=GetXmlHttpObject(); //initializations

alert(xmlhttp);

if(xmlhttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
else{
xmlhttp.onreadystatechange=processData;

var u =document.getElementById("user").value;
var p =document.getElementById("pass").value;

//alert(u+"----"+p);
var url="./scripts/login.asp";
var params="?u="+u+"&p="+p;
alert(url+params);

xmlhttp.open("GET",url+params,true);

xmlhttp.send(null);

}//else
}

I have the same problem...Would you help me Please?

choongseng said...

try to change this:
var url="./scripts/login.asp";

into absolute url and see how it goes.

Unknown said...

Hi All,
I am facing the same problem.
When I post a xmlhttprequest through Chrome I always get status 0,
where as same code work fine through IE.
Can you provide some more information to solve this problem.

thanks.

regards
sumedh

choongseng said...

can you provide your code here and see if we can spot the problem.

Unknown said...

Hi I tried to paste my php server side code here but blog does not allowed to poste script here.

I have posted the problem here you can check

http://www.google.ru/support/forum/p/Chrome/thread?tid=7a89040968141d40&hl=en