Problem with AJAX in FF:synchronous request doesnt work
I have folowing AJAX connector:
function ajaxConnect(url, postData, callBackFunction, mode) {
var xmlObj = new Object();
if (window.XMLHttpRequest) {
xmlObj = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Error initializing XMLHttpRequest");
return;
}
xmlObj.onreadystatechange = function(){
if(xmlObj.readyState == 4){
if (xmlObj.status == 200) {
var elArr = xmlObj.responseXML.getElementsByTagName('root')[0].childNodes;
if (callBackFunction != "") {
callBackFunction(elArr);
} else {
return true;
}
} else {
alert("There was a problem retrieving the XML data");
return false;
}
}
}
xmlObj.open("POST",url,mode);
xmlObj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
if (xmlObj.overrideMimeType) {
xmlObj.setRequestHeader("Connection","close");
}
xmlObj.send(postData);
}
It called from:
var postData = "wordlist="+encodeURIComponent(strWordList);
var url = "/admin/spell/spell_checking.php?Lang="+Lang;
var callBackFunction = spellResultHandle;
ajaxConnect(url, postData, callBackFunction,false);
Where spellResultHandle - some function which handle result of the request and return it. I need to use synchronous mode because of result of spellResultHandle used below in code.
SO THE PROBLEM IS:
This code works in IE, Opera, but doesn't work in FF(1.5.0.6).
If I change mode to asynchronous - works, but it doesn't approach to programm logic.
Any suggestions would be welcome.
TIA
[1802 byte] By [
=vd=] at [2007-11-20 0:14:31]

# 4 Re: Problem with AJAX in FF:synchronous request doesnt work
Sorry if this is old, I just happened accrossed it.
In FF the onreadystatechange event is not fired on a synchronous call and is really not even needed at all in IE either (though it does fire). So for a synchrounous call you could have a function like this:
function getDataNow(url)
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
if(new ActiveXObject("Microsoft.XMLHTTP"))
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
var now=new Date();
if(url.indexOf("?")==-1)
{
var cut=url.substring(0,url.length);
var s=cut+"?c_date="+now;
}
else
{
var cut=url.split('?');
var s=cut[0]+"?c_date="+now+"&"+cut[1];
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
var req=xmlhttp.responseXML;
xml_str_now=xmlhttp.responseText;
}
Basscyst
# 5 Re: Problem with AJAX in FF:synchronous request doesnt work
After more than 3 hours, I found a solution.
Since FF is not calling the onreadystate function and there is actually however a response, we might just have to set the innerHTML manually.
So I added these lines near the end of my ajax code.
if (async==false)
{ document.getElementById(outerhtmlID).innerHTML=xmlHttp.responseText;
}
Here is a little more of my code.
var xmlHttp=GetXmlHttpObject(); // local declaration prevents AJAX REQUESTS RACE (target DOM element is annoyingly overrided) !!
if (xmlHttp==null)
{
alert('Failed server connection! :<');
}
xmlHttp.onreadystatechange=function() { stateChanged(xmlHttp, outerhtmlID); };
xmlHttp.open('POST',url,async);
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
xmlHttp.send(params);
if (async==false)
{
document.getElementById(outerhtmlID).innerHTML=xmlHttp.responseText;
}
alert('async = '+async);
}
abngal at 2007-11-10 4:03:08 >
