- Variety of methods of building fully user-interactive web applications.
(In response to user action on client side, or just on timer, etc.)
to check server database to see if userid is free.
- Small parts of the page can change after initial loading,
rather than entire page being re-loaded.
- XMLHttpRequest - the API to do this.
Ajax is not about XML
- When it emerged, XML was the normal expected return data.
- But in fact, and despite the name "XMLHttpRequest", the return data does not have to be XML.
- JSON return data is common, and some people call it
when they do that
- e.g. Flickr XML and JSON feeds
Can be tricky to write browser-independent, backward-compatible code.
One solution is to use
to take care of this complexity for you.
- I highly recommend
if you are making Ajax calls.
This makes it much, much simpler.
I used to construct the
but I never do any more.
- All my Ajax examples use jQuery to make the call.
- jQuery at w3schools
- jQuery Ajax methods at jquery.com
- See in particular:
No header or footer.
To make it easier to View Source.
Note source loads jQuery.
Coding Ajax takes some getting used to, because it is asynchronous
You fire off a HTTP request.
And then your code carries on
At some future point
the HTTP request returns.
When you make the request, you include the future function to call.
So you can't do:
var x = makeRequest(url); // get back data
// do stuff with x ...
Instead you do something like:
// .. don't do anything with x yet
function fn() // called when request returns in future
x = // .. get return data
// now do stuff with x ...
// and you may need multiple lines like the following in other parts of code:
if ( typeof x == 'undefined' ) ... // x has not yet been defined
else ... // x is defined