Alex Rivera | Logout

How does AJAX work?

Asked 2009-10-02T14:51:58.020
92

What is the essence of AJAX? For example, I want to have a link on my page such that when a user clicks this link, some information is sent to my server without the reloading of the current page. Is that AJAX?

I was able to get this behavior by using isoframes. In more details I put a link (let's say a small image) in a small isoframe. When the user clicks this link the browser reloads only the page in the isoframe.

However, I think it is not an elegant way to reach the goal. I think I have to use AJAX. How does it work? Can a usage of XHTML solve the considered problem in an elegant way? Or do I need to use JavaScripts?

I do not need much. I just want to have a small link which (after clicking) sent some information to server. Let say I have a "star-image" near a message. If a user click the star (he/she likes the message) star changes the color and my server update database (to remember that user likes the message).

Edit
Report

3 Answers

17

AJAX typically involves sending HTTP requests from client to server and processing the server's response, without reloading the entire page. (Asynchronously).

Javascript generally does the submission and receives the data response from the server (traditionally XML, often other less verbose formats like JSON)

The Javascript then may update the page DOM dynamically to update the user's view.

Thus 'Asychronous Javascript And XML'.

There are other options to update the user's view without reloading the page, things like Flash and Applets, but these don't sound like good solutions for your case. Sounds like Javascript is the way to go. There's loads of good library support around, like jQuery as is used on this site, so you don't need to actually write much Javascript yourself.

answered 2009-10-02T14:57:07.090
12

Ajax is more than reload just a part of the page. Ajax stands for Asynchronous Javascript And Xml.

The only part of Ajax that you need is the XMLHttpRequest object from javascript. You have to use it to load and reload small part of your html as a div or any other tags.

Read that example and you will be pro sooner as you think!

<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.myForm.time.value=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","time.asp",true);
xmlhttp.send(null);
}
</script>

<form name="myForm">
Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
Time: <input type="text" name="time" />
</form>

</body>
</html>
answered 2009-10-02T14:59:41.253
2

If you're interested, IBM have a 10 (possibly more) part series on Ajax: Mastering Ajax part 1

Although a few years old now it's a good intro, (even if you just read the first part!)

I think the whole series should be listed Here, although the site's being a bit slow for me at the moment...

Summary:

Ajax, which consists of HTML, JavaScriptâ„¢ technology, DHTML, and DOM, is an outstanding approach that helps you transform clunky Web interfaces into interactive Ajax applications. The author, an Ajax expert, demonstrates how these technologies work together -- from an overview to a detailed look -- to make extremely efficient Web development an easy reality. He also unveils the central concepts of Ajax, including the XMLHttpRequest object.

answered 2009-10-02T15:41:34.787

Your Answer