SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast sffc's Avatar
    Join Date
    Jul 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow onReadyStateChange Firing in Safari and Opera but not in Firefox

    I have a simple XHR request that works well in Safari and Opera but not in Firefox. I have the following code:
    Code JavaScript:
    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?
    "I haven't failed, I just found
    100,000 ways that don't work"
    Thomas Edison

  2. #2
    SitePoint Enthusiast sffc's Avatar
    Join Date
    Jul 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I found my problem. I had been sending my request synchronously instead of asynchronously by accident. Apparently Firefox doesn't fire the readystatechange event on synchronous events. But this error helped me find a more important error in my code
    "I haven't failed, I just found
    100,000 ways that don't work"
    Thomas Edison


Bookmarks

Posting Permissions

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