I have a simple XHR request that works well in Safari and Opera but not in Firefox. I have the following code:
function AJAX(ajaxurl, ajaxdata, ajaxcallback){
if(!ajaxcallback) ajaxcallback=function(){};
var ajaxreqobj = new XMLHttpRequest();
var ajaxpostdata = "";
for(var ajaxi in ajaxdata){
ajaxpostdata += "&"+ajaxi+"="+encodeURIComponent(ajaxdata[ajaxi]);
}
ajaxreqobj.open("POST", ajaxurl, false);
ajaxreqobj.callback = ajaxcallback;
ajaxreqobj.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
this.callback(this.responseText);
}
}
ajaxpostdata = ajaxpostdata.substr(1);
ajaxreqobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if(navigator.userAgent.indexOf("Firefox/3")!=-1){
ajaxreqobj.sendAsBinary(ajaxpostdata);
}else{
ajaxreqobj.send(ajaxpostdata);
}
return ajaxreqobj;
}
var ajaxdata = new Array();
ajaxdata["stage"] = "init";
var ajaxobj = AJAX("ajax.php", ajaxdata, function(){alert("Hello");});
Firebug indicates that “ajax.php” succesfully loaded; in fact, the value of “ajaxobj.readyState” is 4 and “ajaxobj.status” is 200! However, in Safari and Opera, I receive the “Hello” alert box, but in Firefox, I don’t. In fact, no code at all is running inside the “onreadystatechange” event; I know this because I set a breakpoint.
Making Firefox 3 use the standard “ajaxreqobj.send(ajaxpostdata);” does not fix the problem.
What in the world is making my simple AJAX request work in Safari and Opera but not in Firefox?