Section 1.2. Welcome to the new millenium


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 apps

     ResponseThe 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.

Frequently Asked Questions?

Q:

OK, I'm confused. So now we're not using a request/response model ?

A:

Actually, your pages are still making requests and getting responses. You're just using a different approach in how you make those requests and handle the responsesnow you use JavaScript to make the actual request, instead of just submitting a web form.

Q:

Why not just submit the form normally? What does using Ajax really do for us?

A:

The JavaScript code in an Ajax application sends your requests to be processed by the server, but doesn't wait for an answer . Even better, JavaScript can also work with the server's response, instead of reloading the entire page when the server is finished with your request.

Q:

So how does the page ever get back a response?

A:

That's where the asynchronous part of Ajax comes in. When the server sends back a response, JavaScript can update a page with new values, change an image, or even take the user to a whole new web page. And the user never has to wait around while all this is going on.

Q:

So I should use Ajax for all of my requests?

A:

There are still plenty of times when you'll want to use traditional web programming. For instance, when a form is totally filled out, you can let the user click a Submit button, and then send the entire form to your web server, without using Ajax.

But, you'll use Ajax for most of your dynamic page processing, like changing images, updating fields, and responding to users. If you only need to update part of a page, then Ajax is the way to go.

Q:

You said something about XML ?

A:

Sometimes, your JavaScript will use XML to talk back and forth with the server. But you don't always need to use XML for your requests. We'll spend a lot more time looking at when and how you'll use XML in later chapters.

Q:

And AJAX is just an acronym for Asynchronous JavaScript and XML?

A:

Actually, it's "Ajax", and it's not an acronym. Although it sort of looks like one... and it does fit... hmmm. Well, don't ask us, we didn't come up with the term!


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.




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net