firefox reports uncaught exception NS_ERROR_FAILURE with nsIXMLHttpRequest.send

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    firefox reports uncaught exception NS_ERROR_FAILURE with nsIXMLHttpRequest.send

    I am using the XMLHttpRequest to send a request every 5ish seconds or so.
    Everything works fine until I take the server down that the object is trying to retrieve data from.
    Then the firefox console keeps racking up these:
    Code:
    uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)"
    If I turn the server back on, it continues to update it's data just fine. I know a simple solution is to just trap the exception, but I don't think I should have to. There should be something I can do to keep it from generating one in the first place.

    Relevant code:
    [code=javascript]
    function createRequestOb ject()
    {//creates an httprequest object
    var ro;

    var browser = navigator.appNa me;
    if(browser == "Microsoft Internet Explorer"){
    ro = new ActiveXObject(" Microsoft.XMLHT TP");
    }else{
    ro = new XMLHttpRequest( );
    }
    return ro;
    }

    var http = createRequestOb ject();
    var ourinterval=set Interval("SendU pdate()",5000);

    function SendUpdate()
    {
    http.open('get' , '/refresh.cgi?tim e='+ new Date().getTime( ));
    http.onreadysta techange = handleSendUpdat eReponse;
    http.send(null) ;
    }
    function handleSendUpdat eReponse()
    {
    if(http.readySt ate == 4)
    {
    var update = new Array();
    if (http.status==" 200")
    {
    var response = http.responseTe xt;
    if (response!="")
    {
    if(response.ind exOf('|' != -1))
    {
    update=response .split('|');
    if (update.length> 0)
    {
    if (update[1].indexOf("SUCCE SS")>-1)
    {
    var line=update[1];

    var things=new Array();
    things=line.spl it('&');
    var i;
    for (i=0;i<things.l ength;i++)
    {
    var nameval=things[i].split('=');
    if (i>0)
    {
    returnObjById(n ameval[0]).innerHTML=nam eval[1];
    }
    }
    }//end of found "SUCCESS"
    }//end of update split("|") to have multiple sections
    }//end of response.indexo f("|")
    }//end of response!=""
    }//end of status==200
    }//end of readystate=4
    }//end of function
    [/code]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well, turns out that its something firefox just hasn't gotten around to fixing yet:


    The activeX equiv of XMLHttpRequest does not have this problem, providing a much more usefull notification as "12029" in the http.status
    Those error codes can be found here:


    A lot of people use the XMLHTTPRequest object, why am I the only one doing checking for if the talkbalk host is no longer there?

    Here are the changes I made to keep both browsers quiet and happy:
    [code=javascript]
    function SendUpdate()
    {
    var action="DATETIM E&TEMPERATURE&H UMIDITY&SAMPLES TATE";
    action+="&CHAN1 &CHAN2&CHAN3&CH AN4&CHAN5&CHAN6 &CHAN7&CHAN8&CH AN9&CHAN10";
    try
    {
    http.open('get' , '/refresh.cgi?'+a ction+'&amp;tim e='+ new Date().getTime( ));
    http.onreadysta techange = handleSendUpdat eReponse;
    http.send(null) ;
    }
    catch(error)
    {
    //alert(error);
    returnObjById(" SAMPLESTATE").i nnerHTML="Probl em Communicating";
    }

    }

    function handleSendUpdat eReponse()
    {
    if(http.readySt ate == 4)
    {
    var update = new Array();
    if (http.status==" 200")
    {
    var response = http.responseTe xt;
    if (response!="")
    {
    if(response.ind exOf('|' != -1))
    {
    update=response .split('|');
    if (update.length> 0)
    {
    if (update[1].indexOf("SUCCE SS")>-1)
    {
    var line=update[1];

    var things=new Array();
    things=line.spl it('&');
    var i;
    for (i=0;i<things.l ength;i++)
    {
    var nameval=things[i].split('=');
    if (i>0)
    {
    returnObjById(n ameval[0]).innerHTML=nam eval[1];
    }
    }
    }//end of found "SUCCESS"
    }//end of update split("|") to have multiple sections
    }//end of response.indexo f("|")
    }//end of response!=""
    }//end of status==200
    else if(http.status= =12029)
    {
    returnObjById(" SAMPLESTATE").i nnerHTML="Probl em Communicating";
    }
    }//end of readystate=4
    }//end of function
    [/code]

    Comment

    • cleaner
      New Member
      • Jul 2008
      • 1

      #3
      Hi.

      I also had a though time figure out a way to make this happen in firefox.
      Code:
      ////////////////////////////////////////////////////////////////////////////////////////////// 
      // firefox reports uncaught exception NS_ERROR_FAILURE with nsIXMLHttpRequest.send
      // http://bytes.com/forum/thread681905.html
      // http://radio.javaranch.com/pascarello/2006/02/07/1139345471027.html
      // http://dev.rubyonrails.org/ticket/4267      
      // 
      // I needed a way to track server downtime even if the callback does not work when 
      // server closes down. Since firefox does not support failover on server downtime I made this object
      // where I track errors even if there is no callbacks to track.
      // 
      //	To use the object from your main app: 
      //		failover = new clientFailover();   , create object in seperate "thread" (unsing setInterval)
      //		failover.start("fail.php");			, the url where the request is made against as parameter
      // object that handles failover between client and server. It pings the server in smal intervals
      // and reporttext is generated every 10 seconds. Let me know if anyone comes up with a better solution
         
      //////////////////////////////////////////////////////////////////////////////////////////////
      function clientFailover(){
             var start,url,frequest;
             var uinterval = 10000;   // set update interval
             var pinterval = 1000;	// set ping interval
             var intervalID = 0;
             var fstatus = false; // true on error, false on normal operation
             var active = false;  // flag on when thread is aktiv
             var errorflag = 0;
           
           // Set error value code, 0 = connection and callback ok, 1 = connection ok but status error
           // 2 = connection ok but page error, 4 = Connection and callback lost
      	   var setError = function(value){
      			errorflag = value;
            };
            
            var getError = function(){
      		return errorflag;
            };
                     
             // cObject is the XMLHTTPRequest instance
             var cObject = function(){
            		var conn = false;
            		if (window.XMLHttpRequest){
            			conn = new XMLHttpRequest();
            		}else if (window.ActiveXObject){
            			try{
            				conn = new ActiveXObject("Msxml2.XMLHTTP");
            				}catch (e){
            					try{
            					conn = new ActiveXObject("Microsoft.XMLHTTP");
            				}catch (e){
            					conn = false;
            					}
            				}    
                   	 }
                   	return conn;
            }
              // start method, this method is called from the main application
              // Parameter is the URL, This is where the "ping" is making request against
              
              this.start = function(value){
              
              	url = value;
              	frequest = cObject();
              	if (!frequest || !url){
              		return false;
              	}else{
              	  
              	 ping();
              	 // set interval for the update info (every 10 second), this should be higher than the ping interval
              	 setInterval(function(){
              	  			pushInfo();}
              	  			,uinterval);
              	  		
              	       return true;
              	}
              };
            var pushInfo = function(){
            		if (getError() == 4){
            			alert("Server and callback down " + getError());
            		}else{
            			alert("Server and callback works " + getError());
            		}
            };  
              
            // request (ping) the url page on server. The ping interval is set to one second
           var ping = function(){
         
         		intervalID = setInterval(function(){
      				setError(4);
      				frequest.onreadystatechange = check;
      				frequest.open("GET", url, true);
      			try{
      				frequest.send(null);
      		
      			}catch (e){
      				// catch NS_ERROR_NOT_INITZIALIZED
      			}
      		}, pinterval);
      		
            }
           // Check if the request return anything, if so, set status to true meaning that server is down
           var check = function(){
         
      		try{
      			if (frequest.readyState >= 1){
      				
      				if (frequest.status !=200){
      			
      					fstatus = true;
      					setError(2);
      				}else{
      					fstatus = false;
      					setError(0);
      				}
      			}
      		}catch (e){
      			setError(4);
      			// firefox reports uncaught exception NS_ERROR_FAILURE with nsIXMLHttpRequest.send
      			// this failure is catched here
      		}	
      			
            };
            
       }
      Last edited by Dormilich; Jul 18 '11, 08:55 PM.

      Comment

      Working...