A look at how AJAX works
This tutorial will show you how ajax 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 acomplishes 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 recieve data without reloading the page. First, you need to create an object though,
Code:
Firefox: var xmlhttp=new XMLHttpRequest;
IE: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
Code:
xmlhttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
Code:
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;
}
}
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.
