1.2. Welcome to the new millenium!Anybody can program using the same old request/response model. But if you want faster apps that feel like you're working on a desktop, you need something newyou need Ajax, a completely different approach to web programming: 1.2.1. No more waiting around...RequestThe web page sends its requests using a JavaScript function, which handles talking to the server. JavaScriptThis JavaScript code makes a request to the server. Web Server 1.2.2. ...when you're using Ajax appsResponseThe server's response has only the data the page needs... without any markup or presentation. JavaScriptMost of the page doesn't change... only the parts of the page that need to change are updated. UpdateThe JavaScript dynamically updates the web page, without redrawing everything. Sweet! Web ServerFor the web server, nothing has changed; it still responds to each request, just as it did before. Not so fast, buddy... you said these new apps would be more responsive. But we're still waiting around for that JavaScript code to finish running, right? Ajax apps are asynchronous, too If you haven't guessed by now, we're talking about how you can use Ajax to build killer web apps. So far, you've seen how Ajax applications can talk to a web server without the browser reloading and redrawing the entire page all the time. But there's a lot more to Ajax than improved user interfaces. On top of ditching those annoying page reloads, the JavaScript in an Ajax application talks to the web server asynchronously. In other words, the JavaScript makes a request to the server, but you can still type in web forms, and even click buttonsall while the web server is still working in the background. Then, when the server's done, your code can update just the part of the page that's changed. But you'renever waiting around. That's the power of asynchronous requests ! Combine that with updating pages without reloadingthem, and you've got Ajax applications. Don't worry if you didn't get all this; we'll talk a lot more about asynchronous programming in the coming chapters.
Jesse James Garrett, who came up with the term "Ajax", said he didn't mean for it to be an acronym. Go figure! Umm, excuse me... if you're done with all this theory, I think I could use some of that Ajax over here. Now that I'm selling my custom snowboards online, I put together a web form that keeps up with my sales. But every time I want an update, the whole screen reloads. You think all this Ajax stuff can help? Katie, snowboard queen and web entrepreneur. Here's the current, non-Ajax version of Katie's app, which is just a web form that submits requests to a PHP script. The PHP script looks up how many boards have been sold. Then the PHP script uses the price Katie sells her boards for, and what each board costs her, to figure out how much money she's made. |