Difference between asynchronous (AJAX) and synchronous request?

Now-a-days, this is a very common question that been asked in all most every basic interview – What is the main strength of AJAX? Or why and when should we use AJAX in web application?
And the most common answer is – “AJAX does not refresh or reload the whole page”.
But, the more perfect answer is – “AJAX is an asynchronous technology where others request are synchronous.”
So, what is the difference?
In a word, a program does not wait for response after requesting an asynchronous call whereas synchronous does so.
Here is a simple example –
function check() {
var a=0;
a = getStatus(“getstatus.php?id=5”);
if(a==1) {
alert(“active”);
} else {
alert(“not active”);
}
}
Here getStatus() function sends a AJAX request to the server with “getstatus.php?id=5” url and the php file decides (from database may be) the status and output/response as 1 or 0.
But, this function will not work properly. It will alert “not active” instead of “active”. And yes, that is for the asynchronous request.
The reason is – when a = getStatus(“getstatus.php?id=5”); line is being executed program does not wait for the response of setStatus() function. So, value of keep unchanged or set to null.
So, how should we work with asynchronous request?
Of course, using callback function. Callback function is that function which is triggered when the request completes to get the response (or as defined).

Refer link:- http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/ 

Comments

Post a Comment

Popular posts from this blog

How do I compare all the column values based on the Partition by with ID