4 ms·
AJAX, how it's commonly referred to now, is simply the process of loading content from an external page into a Javascript variable. Most of the time it's not e
by colinsidoti 16y ago
AJAX, how it's commonly referred to now, is simply the process of loading content from an external page into a Javascript variable. Most of the time it's not even AJAX anymore, but rather AJAJ (Asynchronous Javascript and JSON). In terms of getting started there really isn't much to learn, especially if you have some experience with jQuery.
The big thing you need to remember is the A: Asynchronous. Especially if you're new to programming, this won't make any sense to you. As you use it you'll just get frustrated about why it works the way it does. It takes a bit of experience to learn to use it well, so give it some time. Remember these two key tips:
1. The code following your AJAX call should not be dependent on the content you are loading. Javascript will continue running line by line after the AJAX request is sent, NOT after the content has been loaded. If any code is dependent on the loaded content, it belongs in the return function.
2. The return function for an AJAX call only has the data from the page it loaded, it cannot* see the variables you defined before you made the request.
Other than that, just realize that the true power of AJAX comes from the data you're loading in. You can use it to create an <iframe> of sorts that simply loads pre-built HTML files into a div, but that's not all that exciting. If you use something like PHP to process and load data, you can start to make some very cool applications. If you're looking for a good learning project, I would recommend creating a simple "shoutbox" that works completely with AJAX, including periodic refreshes to check for new comments.
If you don't know what a shoutbox is: http://www.shoutmix.com/main/ http://www.shoutmix.com/main/
Some jQuery functions to check out: .load(), .post(), .get(), .ajax()
*: A lot of times, your return function WILL be able to see variables you defined before the AJAX request because they ended up in the global scope. I'd recommend that you don't rely on this unless your making the variable global on purpose. Otherwise, there's a lot of potential for a debugging nightmare.
- bartl 16y ago"You can use it to create an <iframe> of sorts that simply loads pre-built HTML files into a div, but that's not all that exciting. If you use something like PHP to process and load data, you can start to make some very cool applications." The prototypical example for use of AJAX is to submit a HTML form without ever leaving the page (or at least, do something equivalent), for example the vote buttons on forums like, well, this one here.