Ajax responses very slow.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anubhavjha
    New Member
    • Jul 2008
    • 22

    Ajax responses very slow.

    Hi Friends,

    I have a web application through which, the ajax is used to post the data onto the server and return the results. When i alert the xmlhttp.readyst ate sometimes it takes long time, that the process looks like freezing and sometimes its done very quickly as it is designed to.

    I checked by alerting xmlhttp.readyst ate.

    sometimes i get response 1,2 ,3 4 as the alert box quickly.
    and sometimes i just get 1 and then nothing happens.

    Has any one experienced the same.

    Regards,
    Anubhav
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    maybe problem with your PHP file. Try to send the same variables to the file through a manual call (enter it in your browser address bar), it maybe that this file never gets back.

    Only thing I can think of that causes this.



    Dan

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Just try to get the response from a dummy PHP file...

      If the response time is same for every call.. then as dlite922 told its a problem with your PHP... Prpbably you need to fix some issues..

      Regards
      Dheeraj Joshi

      I did not faced such problems.

      Comment

      • anubhavjha
        New Member
        • Jul 2008
        • 22

        #4
        I checked that , but if i send the variables directly to the php file as the action in the form tag, it goes out fine. I am using IE 7.0 and sometimes it works fine and sometimes does not work.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Check it in FF(Fire Fox)... I got some problem when i used AJAX call in IE(Though not similar)...

          Regards
          Dheeraj Joshi

          Comment

          • anubhavjha
            New Member
            • Jul 2008
            • 22

            #6
            Hi Dheeraj,, even firefox and ie shows the same response. and there is a javascript code written that the web application cannot be used on a firefox. can you think of something else.

            Comment

            • dgreenhouse
              Recognized Expert Contributor
              • May 2008
              • 250

              #7
              Do think that multiple requests are being fired off?

              If so, that may part of the bottleneck.

              If I remember correctly, IE will open only 2 connections at once.

              You might also want to add a timeout in your Ajax callback function so the application is more responsive.



              Post your httpRequest and callback function.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                that seems more to be a problem with the JavaScript-code then with the php-code. could you post the JavaScript-code that is relevant for the ajax-call?

                kind regards

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Ok.... I dont know any other solution.

                  Just post the Java script like GITS told... It may be the culprit....

                  Regards
                  Dheeraj Joshi

                  Comment

                  • anubhavjha
                    New Member
                    • Jul 2008
                    • 22

                    #10
                    Originally posted by dgreenhouse
                    Do think that multiple requests are being fired off?

                    If so, that may part of the bottleneck.

                    If I remember correctly, IE will open only 2 connections at once.

                    You might also want to add a timeout in your Ajax callback function so the application is more responsive.



                    Post your httpRequest and callback function.
                    Thank you for your reply.

                    I dont know to check if the multiple requests are being fired off.can you pls provide detail on it.

                    Comment

                    • anubhavjha
                      New Member
                      • Jul 2008
                      • 22

                      #11
                      My codes.

                      First i was using the following codes:-

                      The function is called on submit of a button.

                      Code:
                      function submitform()
                      {
                      result=0;
                      //gettting the form values;
                      var requestid=document.getElementById("requestid").value;
                      var sdenggname=document.getElementById("sdenggname").value;
                      var sla=document.getElementById("sla").value;
                      var helpdesk_comments=document.getElementById("helpdesk_comments").value;
                      var calltype=document.getElementById("calltype").value;
                      //validation part
                      if(sdenggname=="--"||sla=="--"||helpdesk_comments==""||calltype=="--")
                      {
                      alert("Please make sure that you have filled in all the fields");
                      return false;
                      }
                      //validateion part end
                      var domain=document.domain;
                      
                      var url = "http://"+domain+"/crfportal/assignhelpdeskcalls_submit.php"; //url
                      
                      var params = "requestid="+requestid+"&sdenggname="+sdenggname+"&sla="+sla+"&helpdesk_comments="+helpdesk_comments+"&calltype="+calltype; //generating parameters
                      
                      //displaying the user with a statment while the request is being sent.
                      document.getElementById("d1").innerHTML="<table><tr><td>Please wait while the call is getting assigned</Td></tr></table>"
                      var xmlhttp=false;
                      //alert(params);
                      /*@cc_on @*/
                      /*@if (@_jscript_version >= 5)
                      // JScript gives us Conditional compilation, we can cope with old IE versions.
                      // and security blocked creation of the objects.
                       try {
                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                       } catch (e) {
                        try {
                         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (E) {
                         xmlhttp = false;
                        }
                       }
                      @end @*/
                      if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                      	try {
                      		xmlhttp = new XMLHttpRequest();
                      	} catch (e) {
                      		xmlhttp=false;
                      	}
                      }
                      if (!xmlhttp && window.createRequest) {
                      	try {
                      		xmlhttp = window.createRequest();
                      	} catch (e) {
                      		xmlhttp=false;
                      	}
                      }
                        	//alert("xmlhttp"+xmlhttp);
                       try{xmlhttp.open("POST", url, true);}
                       catch(e)
                       {
                            alert("some error");
                      }
                      
                      //Send the proper header information along with the request
                      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                      xmlhttp.setRequestHeader("Content-length", params.length);
                      xmlhttp.setRequestHeader("Connection", "close");
                      
                      xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
                      
                      	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                      		
                      		result=xmlhttp.responseText;
                      		if(result!=0)
                      		{
                      		window.location="assignsdcalls.php?refresh=1";
                      		}
                      	}
                      }
                      xmlhttp.send(params);
                      }

                      Then i changed the above code to the following:

                      Code:
                      function submitform()
                      {
                      result=0;
                      //gettting the form values;
                      var requestid=document.getElementById("requestid").value;
                      var sdenggname=document.getElementById("sdenggname").value;
                      var sla=document.getElementById("sla").value;
                      var helpdesk_comments=document.getElementById("helpdesk_comments").value;
                      var calltype=document.getElementById("calltype").value;
                      var status=document.getElementById("reqstatus").value;
                      var sdenggcomment=document.getElementById("sdenngcomments").value;
                      //document.form1.submit();
                      //validation part
                      if((status=='Closed'||status=='closed')&& sdenggcomment=="")
                      {
                      alert("The call under closed status need to have comment")
                      return false;  	
                      }
                      if(sdenggname=="--"||sla=="--"||helpdesk_comments==""||calltype=="--")
                      {
                      alert("Please make sure that you have filled in all the fields");
                      return false;
                      }
                      //validateion part end
                      //setTimeout("",1250);
                      document.getElementById("assigncall").disabled=true;
                      var domain=document.domain;
                      
                      var url = "http://"+domain+"/crfportal/assignhelpdeskcalls_submit.php"; //url
                      //var url1 = "http://172.16.140.15/crfportal/assignhelpdeskcalls_submit.php"; //url to be used when the 172.16.140.15 is there.
                      var params = "requestid="+requestid+"&sdenggname="+sdenggname+"&sla="+sla+"&helpdesk_comments="+helpdesk_comments+"&calltype="+calltype+"&status="+status+"&sdenggcomment="+sdenggcomment+ "&rand=" + (new Date()); //generating parameters
                      document.getElementById("d1").innerHTML="<table><tr><td>Please wait while the call is getting assigned</Td></tr></table>"
                      var xmlhttp=false;
                      xmlhttp=new ajaxRequest();
                       try{xmlhttp.open("POST", url, true);}
                       catch(e)
                       {
                      try{xmlhttp.open("POST", url1, true);}
                      catch (e)
                      	 {
                            alert("some error");}
                       }
                      
                      //Send the proper header information along with the request
                      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                      //xmlhttp.setRequestHeader("Content-length", params.length);
                      //xmlhttp.setRequestHeader("Connection", "close");
                      
                      xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
                      //alert(xmlhttp.readyState);
                      
                      	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                      		result=xmlhttp.responseText;
                      		if(result!=0)
                      		{
                      		//document.getElementById("d1").innerHTML="<table><tr><td>"+result+"</Td><td><a href='assignsdcalls.php'>Assign another calls</a></td></tr></table>";
                      //document.getElementById("assigncall").disabled=true;
                      window.location="assignsdcalls.php?refresh=1";
                      		}
                      	}
                      }
                      xmlhttp.send(params);
                      
                      }
                      The function ajaxrequest is defined as follows:-

                      Code:
                      function ajaxRequest(){
                       var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
                       if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
                        for (var i=0; i<activexmodes.length; i++){
                         try{
                          return new ActiveXObject(activexmodes[i])
                         }
                         catch(e){
                          //suppress error
                         }
                        }
                       }
                       else if (window.XMLHttpRequest) // if Mozilla, Safari etc
                        return new XMLHttpRequest()
                       else
                        return false
                      }
                      when i give alert on xmlhttp.readyst ate sometimes i get alert as 1234 very quickly and sometimes it just stops after 1.

                      Also after going through my code, if you see any thing which is not a good programming practice. please let me know.
                      Last edited by Dormilich; Aug 27 '09, 06:19 AM. Reason: Please use [code] tags when posting code

                      Comment

                      Working...