Ajax and FireFox problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Ajax and FireFox problem

    I have a problem with some ajax communications on some users' computers.

    Sometimes data sent via ajax would not post to the database. I resolved the problem by deleting the user's profile and creating a new profile.

    My dilemma is this, I don't know what part of the profile was creating the problem, so I don't know if it will reappear?

    Here is what I do know (see my code below).

    1. The query was always present in the query string being sent
    2. The web server (Apache 2.2.10) shows a 200 code in the access log even though no post occurred on the database (MySQL 5.0 MyISAM storage engine).
    3. If I set a breakpoint with firebug on line 51 (the test for the readyState) I could get it to post properly.

    So it appears as if the script is running a little ahead thereby creating an incomplete query. And it is probably returning the 200 code because there is nothing on the server side app to trap and error if nothing occurs. That however is besides the point, the problem is the client side script in conjunction with the user's profile.

    I know you can programatically control the user's firefox profile, but I am not sure which part I need to check or even how to check it?


    Code:
    var timeHttp;
    
    function submitTime()
    {
    	if (window.ActiveXObject) 
    	{
    		timeHttp = new ActiveXObject("Microsoft.XMLHTTP");
    	}else if (window.XMLHttpRequest) {
    		timeHttp = new XMLHttpRequest();
    	}
    	var url = "/<myPath>/<myApp>?timeStamp=" + new Date().getTime();
    	var queryString = buildTime();
    	timeHttp.onreadystatechange = handleTimeStateChange;
    	timeHttp.open("POST", url, true);
    	timeHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //---------------------------------
    When stopped here
    query was always present
    //---------------------------------
    	timeHttp.send(queryString);
    }
    function buildTime()
    {
    	qString = "Action="+document.getElementById("MySubmit").value; 
    
    	for (var i = 0; i < aDailyTime.length; i++ )
    	{
    		qString += "&RECKEY"+i+"="+aDailyTime[i].RECKEY;
    		qString += "&EMPNO"+i+"='"+aDailyTime[i].EMPNO+"'";
    		qString += "&WORKDAY"+i+"="+aDailyTime[i].WORKDAY;
    		qString += "&PRJCTNAME"+i+"='"+encodeURIComponent(aDailyTime[i].PRJCTNAME)+"'";
    		cTotTime = parseFloat(aDailyTime[i].HOURS)+parseFloat(aDailyTime[i].minutes);
    		qString += "&STARTMILES"+i+"=";
    		qString += ( aDailyTime[i].STARTMILES == "" ) ? 0 : aDailyTime[i].STARTMILES;
    		qString += "&ENDMILES"+i+"=";
    		qString += ( aDailyTime[i].ENDMILES == "" ) ? 0 : aDailyTime[i].ENDMILES;
    		//if "--->" is found in project name then this employee's departmetn is not assigned to this job's department
    		cIsDept  = ( aDailyTime[i].PRJCTNAME.substring(0,4) == "--->" ) ? 'F' : 'T';
    		qString += "&isDept"+i+"='"+ cIsDept +"'";
    	}
    	qString += "&method=POST";
    	return qString;
    }
    
    function handleTimeStateChange() 
    {
    //----------------------------------------
    This is where the problem would
    occur.  Not sure why?
    //---------------------------------------
    	if (timeHttp.readyState == 4) 
    	{
    		if (timeHttp.status == 200) 
    		{
    			parseTimeResults();
    		}
    	}
    }
    
    function parseTimeResults() {
    
    	var returnedString = timeHttp.responseText;
    	var aReturn = new Array();
    	aReturn = returnedString.split(';');
    	if ( aReturn[0] == "An error occured") 
    	{ 
    		eAlert  = aReturn[0]+"\n"
    
    		if ( aReturn[1] == "Error on time submittal") { 
    			eAlert += aReturn[1]+" "+document.getElementById("EMPNO").value
    		}else{ 
    			eAlert += aReturn[1]+"\n"
    			eAlert += aReturn[2]+"\n"
    			eAlert += aReturn[3]+"\n"
    			eAlert += aReturn[4]
    		}
    
    		alert( eAlert );
    
    	}else{ 
    		alert( aReturn[0] );
    	}
    }
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Since you deleted and created a new profile, has the problem ever occurred again?

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Originally posted by acoder
      Since you deleted and created a new profile, has the problem ever occurred again?
      Yes the problem did re-occur. But just now I have been able to solve the problem. As usual it was within my own code although I still do not understand what or why it did what it did.

      I was combining some of my pre-ajax code with my current ajax code when submitting a record.

      My action property for the form was set at action = "return false;" to prevent the enter button from submitting the form. This is standard in all my apps as I do some validation on the form before submitting. But in using the ajax approach to send only the data and not the form to the web server, the faulty code crossed the function with the "form.submit(); " line which it should not have. There was no specific action or url for the form.submit() to execute but it seemed to mess up the "readyState " on the ajax where it seems nothing was submitted at all. I can accept and understand all that. But what baffles me is the access log on the web server shows that the receiving program was called and executed with a return code of 200. Well I have eliminated the cross link and the problem has been solved.

      Thank you for checking in on me

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by Claus Mygind
        My action property for the form was set at action = "return false;" to prevent the enter button from submitting the form. This is standard in all my apps as I do some validation on the form before submitting.
        Not sure exactly what the problem might have been, but I can say that there's no need for this as long as you call your validation function onsubmit:
        Code:
        <form ... onsubmit="return validate();">
        If you return false when validation fails, then the form would not be submitted, thus you can set a correct action and don't need to call the submit() method.

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Thanks for your help.

          Comment

          Working...