This post is copied from my first blog which was posted almost 4 years ago.
Now-a-days, this is a very common question that being asked in most interview for beginners level developers – 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).
perfect
Nice Understood
It’s a shame you don’t have a donate button! I’d definitely donate to this outstanding blog!
I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.
I look forward to new updates and will talk about this
website with my Facebook group. Chat soon!