As a prerequisite to actually using AJAX in practice, you should know how to do some server-side programming using something like PHP, Ruby on Rails, etc. You should also be a bit familiar with Javascript.
All AJAX does is make a little request to the server without reloading the page. Here's some code:
$.get("profile.php", { username: "rob" },This simple example makes a request to profile.php with the parameters username = "rob", and drops the text into a div on the current page. This is effectively the same as accessing profile.php?username=rob directly, except without refreshing the page.
function(response){
// put the HTML content of profile.php into a div with id = "profile_page"
$("#profile_page").html(response);
}
);
You can also do POST requests for mutable actions:
$.post("login.php",The login action here would just output "good" if the login worked, or "bad" if the login didn't work.
{ username: "rob", password: "secret" },
function(response){
if (response == "good"){
alert("You have logged in successfully.");
}else{
alert("Invalid username/password");
}
}
);
There, you're now an AJAX master! Of course it would be better to learn by trying it out for yourself, but that's the basics of it all.
5 comments:
Excellent little tutorial. It amazes me how tutorials normally make Ajax seem amazingly complex, but with jquery, it's really simple.
AJAX is very cool when able to fit into a working scheme.
I loved your website content with learning a new way of using AJAX!
Thanks for sharing.
Norman Flecha
STRAIGHTALK
http://www.straightalk.biz
Nice one, thanks for that.
Also, $.json(url, callback) will grab JSON from not only your server but any other domain with JSON objects. Yay
Thanks Rob, this really is ajax in 5 minutes!
Post a Comment