This tutorial will show you how ajax (Asynchronous Javascript and XML) works and how to use it from a web developers standpoint. Ever wonder how things like G-Talk work? they don’t reload the page or use Iframes and yet they always appear to have the data thats coming in instantly. It accomplishes this using ajax. Ajax is not a language of its own, so if you know javascript and HTML, your all set. This is where it all comes from, the ability to send and receive data without reloading the page. First, you need to create an object though,
Firefox: var xmlhttp=new XMLHttpRequest;
IE: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
The best way I have found to get them both in one command is:
xmlhttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Micro
soft.XMLHTTP");
Now you need to create a callable function as well as an onreadystatechange handler. This part is simple. Here’s an example:
function getnamedata()
{
xmlhttp.open("POST","file.php");
xmlhttp.onreadystatechange=handler;
xmlhttp.send("name=random&id=42");
}
function handler()
{
if(xmlhttp.readyState==4)
{
alert("Name Info: "+xmlhttp.responseText;
xmlhttp.close;
}
}
The xmlhttp.open call opens a connection to the page, the first parameter is POST or GET depending on the method you want to use. The second parameter is the filename of the page relative to the current directory. Line 2 tells it that whenever it goes through a statechange to call a certian function, the handler. xmlhttp.send passes variables to the script that you are calling, you’ll notice that it passes them in almost exactly the same way as a url bar minus the ‘?’.
The handler function gets called every time that xmlhttp changes state. We have the if statement so that it will only execute when readyState==4 (4 is completed). The last statement of course retreives the data from the xmlhttp object. If you are getting most types of data you should use the responseText property, for actual XML data use the responseXML property. The last statement just closes the socket for later use. Using this as a basic model you can make an entire website live if you want to, or just find something fun to do with it.
This is only a beginner level coding. But if you are interested and want to learn more then my good friend ChronoTron has an Article : Repository of tutorials and resources on Ajax.
Related posts:












Blog
Nice post!! Neat and simple does the trick!
thanks chrono
dsadas
I love this site. Good work…
Its really gud for beginers and helped me lot.
Thank you.
Regards,
-Srini
this site helped me a lot…
u rock my world!!!
hell yeah
ok the first one was just a test…
now i see it worked realy damn fast…
u really really rock my world!!!
peace brother…
ty
much appreciated article.
It’s really good stuff for the beginers
HEY,,THANKS FOR DESCRIBING AJAX IN SUCH AN EASY LANGUAGE,,I HAVE SEARCHED A LOT,,BUT I FOUND IT VERY SIMPLE AS COMPARED TO ANOTHER ARTICLE AVAILABLE IN INTERNET
KEEP ON THIS GOOD WORK.
SO,,,
THANKS A LOT,,
A neat piece of information – That really helps the beginners to understand the concept behind AJAX in a much simpler manner.
Hi
I have fully understood through reading
Thanks a lot.
Very useful and very good
Thanks
i have read chrono’s article yet, jst my contents
Nice post
Very short description. Wud have been better if you add some more examples.
Hi,
It is really nice article for beginners and it gave me good understand of what AJAX is and how it works.
But it would be better if you post the internal functionality of AJAX and how XML involves in it.
Thanks.