A look at how AJAX works

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:

  1. AJAX
  2. Ajax Repository
  3. Ajax conference online.
  4. Generators in Python….
  5. Secure PHP Programming!

29 Responses to “A look at how AJAX works”

  1. Chrono Cr@cker 20. May, 2006 at 2:34 pm #

    Nice post!! Neat and simple does the trick!

  2. gapp 23. May, 2006 at 4:00 am #

    thanks chrono

  3. assdad 12. Jun, 2006 at 3:42 pm #

    dsadas

  4. fenton 14. Jul, 2006 at 5:40 am #

    I love this site. Good work…

  5. Srini from India 03. Oct, 2006 at 2:01 pm #

    Its really gud for beginers and helped me lot.
    Thank you.

    Regards,
    -Srini

  6. charleee 07. Nov, 2006 at 1:10 pm #

    this site helped me a lot…

    u rock my world!!!

    hell yeah

  7. charleee 07. Nov, 2006 at 1:11 pm #

    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

  8. anuraj 14. Apr, 2007 at 6:29 am #

    much appreciated article.

  9. Gangula 08. May, 2007 at 4:18 am #

    It’s really good stuff for the beginers

  10. divya 11. Jun, 2007 at 6:48 am #

    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,,

  11. sanjay 26. Jun, 2007 at 9:19 am #

    A neat piece of information – That really helps the beginners to understand the concept behind AJAX in a much simpler manner.

  12. S.Indirakumar 09. Jul, 2007 at 9:10 am #

    Hi
    I have fully understood through reading
    Thanks a lot.

  13. kashyap 28. Oct, 2007 at 3:50 pm #

    Very useful and very good
    Thanks

  14. Imran 20. Nov, 2007 at 3:16 pm #

    i have read chrono’s article yet, jst my contents

  15. Dharmendra 09. Apr, 2008 at 1:43 pm #

    Nice post

  16. krishna 24. Jul, 2008 at 7:01 pm #

    Very short description. Wud have been better if you add some more examples.

  17. Ajay 27. Nov, 2008 at 11:11 am #

    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.

Trackbacks/Pingbacks

  1. Simple AJAX. | AmerM - 26. Jun, 2007

    [...] Article Source [...]

  2. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] by levoltz Mr. Shang Shin who works for the Sun Microsystems is offering a 10 week free course on AJAX programming. The course is already half way thro’ (It started from August 4th, 2006) goes on till [...]

  3. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] by levoltz Mr. Sang Shin who works for the Sun Microsystems is offering a 10 week free course on AJAX programming. The course is already half way thro’ (It started from August 4th, 2006) goes on till [...]

  4. How-To: Create a Wordpress Theme in 5 minutes - LeVoltz - 30. Jul, 2009

    [...] 1. You cannot upload the new template if your blog is hosted at wordpress.com. 2. How to Ajax -ify your wordpress template – coming [...]

  5. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] pretty much disappointed to find little appreciation for their work. For instance the session on Ajax-ifying your blog by Boskey didn’t find enough [...]

  6. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] Update On 09.10.06, In Graphics, by levoltz Bhasker V Kode (Bosky) discussed about the basics of Ajax and Twidgets in his session. He introduced to the crowd in the conference hall, a few of the [...]

  7. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] On 08.23.06, In Graphics, by levoltz Radius IM is the new all ajax online messenger of the block. You can sign up into your yahoo messenger, MSN messenger, AOL and G [...]

  8. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] pitch in the discussions and presentations, but if you look beyond you will get a good idea what AJAX is [...]

  9. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] On 04.22.06, In Graphics, by levoltz Shreyas at Chrono Tron has a brilliant repository of Ajax tutorials. If you have no clue about Ajax/planning to learn Ajax/newbie to Ajax, this is the place to get [...]

  10. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] Messenger, Google Talk, MSN Messenger, AOL, Jabber and ICQ. And it is website based, written on Ajax, so as long as your browser supports it, you can IM from any computer, anywhere and with any [...]

  11. LeVoltz – Tips, Hacks, Cracks, Tutorials, downloads, softwares - LeVoltz - 30. Jul, 2009

    [...] In .net, AJAX is implemented as “Atlas” and in J2EE, any app server that supports servlets will support AJAX. [...]

  12. What is the best advertisment channel my site can use? - - Default Dr. - 12. Aug, 2009

    [...] ___________________________________________________________________ how to AJAX | Ten Crucial Keys to Building a High Traffic Website | Bypass Proxy courtesy Your [...]

Leave a Reply