Problem with AJAX in FF:synchronous request doesn't work
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Problem with AJAX in FF:synchronous request doesn't work

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    7

    Question Problem with AJAX in FF:synchronous request doesn't work

    I have folowing AJAX connector:

    Code:
    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:
    Code:
    	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

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Problem with AJAX in FF:synchronous request doesn't work

    Hi,

    Well, AJAX is asynchronous (Asynchronous Javascript And XML), why would you want to use it for synchronous communication? Why not just submit your page as you would ussually do (submit to the server and get the response), because how much value can you actually be getting from using AJAX this way? A hard to maintain system that works exactly the same as the 'normal' web model. What I mean by this is that you have to write a whole lot of buggy javascript to handle something that can be acheieved by just submitting to the server and receiving the response.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Aug 2005
    Location
    Milwaukee, WI
    Posts
    55

    Re: Problem with AJAX in FF:synchronous request doesn't work

    Don't be too hard on him Bnt. There are times when such synchronous requests are useful within the scope of a larger application and such classes as he developed that handle both asychronous and synchronous communication are nice to have around. Especially if you're working with the concept of modal windows. If you look closer, you'll notice that, while the synchronous call does make you wait for the result to come back before you can do anything, you still don't necessarily have to refresh or load a whole new page, but rather can take the results and insert them using the DOM. This is still also within the scope of AJAX. Asynchronous Javascript And XML was assigned to the term AJAX by people who didn't know what they were talking about, according to the man who originally coined the term. AJAX is a design pattern, and does not require asynchronicity.
    Last edited by gitter1226; August 13th, 2006 at 08:27 PM.

  4. #4
    Join Date
    Aug 2006
    Posts
    1

    Re: Problem with AJAX in FF:synchronous request doesn't work

    Why not have a try under the debug mode by FIREFOX+FIREBUG
    Last edited by o0JSP; August 14th, 2006 at 11:10 AM.

  5. #5
    Join Date
    Sep 2006
    Posts
    1

    Re: Problem with AJAX in FF:synchronous request doesn't 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:

    Code:
    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
    Last edited by Basscyst; September 14th, 2006 at 08:02 PM.

  6. #6
    Join Date
    Oct 2007
    Posts
    1

    Smile Re: Problem with AJAX in FF:synchronous request doesn't 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);
    }

  7. #7
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,867

    Re: Problem with AJAX in FF:synchronous request doesn't work

    Welcome to the forums abngal. You will note that you have posted on a subject over a year old. Please make your posts more relevant. Thank you!
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center