By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

How to call AJAX synchronously

RamananKalirajan
100+
P: 607
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
Jul 28 '08 #1
Share this Question
Share on Google+
6 Replies


gits
Expert Mod 5K+
P: 5,343
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:

Expand|Select|Wrap|Line Numbers
  1. request.open("GET", url, false);
kind regards
Jul 28 '08 #2

RamananKalirajan
100+
P: 607
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:

Expand|Select|Wrap|Line Numbers
  1. request.open("GET", url, false);
kind regards
Thank You sir, for your timely help.

Regards
Ramanan Kalirajan
Jul 28 '08 #3

gits
Expert Mod 5K+
P: 5,343
no problem ... could you solve the issue with that?

kind regards
Jul 28 '08 #4

RamananKalirajan
100+
P: 607
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="javascript">
var counter=0;
$('myButton').observe('click',function(){
new ConnectionManager();
});

var ConnectionManager = Class.create({
initialize : function(){
alert("Inside Constructor");
this.callFunction();
},

callFunction : function(){
this.connect("MyNew.html",function(transport){
$('container').insert(transport.responseText+count er);

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

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

Any Suggestions?

Regards
Ramanan Kalirajan
Jul 29 '08 #5

RamananKalirajan
100+
P: 607
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="javascript">
$('myButton').observe('click',function(){
var myObj = new ConnectionManager();
myObj.getConnectionManager("MyNew.html",function(t ransport){
$('container').insert(transport.responseText);
});
});

var ConnectionManager = Class.create({
initialize : function(url,myFunction){
new Ajax.Request(url,{
method: 'get',
onSuccess:myFunction
});
},
getConnectionManager : function(url,myFunction)
{
var con = new ConnectionManager(url,myFunction);
return con;
}
});
</script>[/HTML]

Please have a look over it.

Regards
Ramanan Kalirajan
Jul 29 '08 #6

gits
Expert Mod 5K+
P: 5,343
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
Jul 29 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.