How to call AJAX synchronously

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    How to call AJAX synchronously

    Hi guys I am trying to find a solution for making the Ajax call synchronous. I am having a AJAx. I am making five ajax calls but it must work in the fashion after completing first it should call second and then third likewise it must move on. Do any one have an Idea about that.

    Regards
    Ramanan Kalirajan
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    it doesn't need synchronous for that requirement ... just start the dependend request from the callback of each request that is needed before ...

    but in case you want the sync mode with all its drawbacks just change the third param in the request's open-method to false:

    [CODE=javascrip]request.open("G ET", url, false);[/CODE]
    kind regards

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Originally posted by gits
      it doesn't need synchronous for that requirement ... just start the dependend request from the callback of each request that is needed before ...

      but in case you want the sync mode with all its drawbacks just change the third param in the request's open-method to false:

      [CODE=javascrip]request.open("G ET", url, false);[/CODE]
      kind regards
      Thank You sir, for your timely help.

      Regards
      Ramanan Kalirajan

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        no problem ... could you solve the issue with that?

        kind regards

        Comment

        • RamananKalirajan
          Contributor
          • Mar 2008
          • 608

          #5
          Ya I solved but the problem is my lead is expecting the class to be an singleton one.

          My Code:
          [HTML]<html>
          <form>
          <input type="button" id="myButton" value="Click Me">
          <br/>
          <div id="container"> </div>
          </form>
          </html>
          <script src="prototype. js"></script>
          <script language="javas cript">
          var counter=0;
          $('myButton').o bserve('click', function(){
          new ConnectionManag er();
          });

          var ConnectionManag er = Class.create({
          initialize : function(){
          alert("Inside Constructor");
          this.callFuncti on();
          },

          callFunction : function(){
          this.connect("M yNew.html",func tion(transport) {
          $('container'). insert(transpor t.responseText+ counter);

          alert("End of Call"+counter);
          });
          if(counter<5)
          this.callFuncti on();
          },

          connect : function(uri, myFunction){
          counter++;
          alert(counter);
          new Ajax.Request(ur i,{
          method: 'get',
          onSuccess : myFunction
          });
          }
          });
          </script>[/HTML]

          Any Suggestions?

          Regards
          Ramanan Kalirajan

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            Hi Mr. Gits may be this code could do synchronosuly. I have implemented singleton concept here


            [HTML]<html>
            <form>
            <input type="button" id="myButton" value="Click Me">
            <br/>
            <div id="container"> </div>
            </form>
            </html>
            <script src="prototype. js"></script>
            <script language="javas cript">
            $('myButton').o bserve('click', function(){
            var myObj = new ConnectionManag er();
            myObj.getConnec tionManager("My New.html",funct ion(transport){
            $('container'). insert(transpor t.responseText) ;
            });
            });

            var ConnectionManag er = Class.create({
            initialize : function(url,my Function){
            new Ajax.Request(ur l,{
            method: 'get',
            onSuccess:myFun ction
            });
            },
            getConnectionMa nager : function(url,my Function)
            {
            var con = new ConnectionManag er(url,myFuncti on);
            return con;
            }
            });
            </script>[/HTML]

            Please have a look over it.

            Regards
            Ramanan Kalirajan

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              i cannot see whether this is sync or async ... from this code ... it seems more an async use ... but i'm not familiar with prototype ... and as i said ... the correct call-chain of async request would be enough and you don't need it sync ... in case it works ... then it could be ok :) ...

              kind regards

              Comment

              Working...