Chapter 3

Creating Ajax Applications

IN THIS CHAPTER

  • Creating XMLHttpRequest objects
  • Configuring XMLHttpRequest objects
  • Handling data downloads from the server using anonymous functions
  • Fetching text data from the server
  • Passing data to the server using Ajax and the GET and PUT HTTP methods
  • Fetching and decoding XML data from the server
  • Fetching XML data from the server by passing data to the server

It's time to write some Ajax. In this chapter you see how to write complete Ajax applications from soup to nuts. In particular, you're going to see how to create and work with the XMLHttpRequest object. Because this object is the central object on which Ajax is based, the XMLHttpRequest object is going to be at the very heart of all the Ajax applications you write.

Writing Ajax

This chapter starts by taking an in-depth look at the example you first saw in Chapter 2 — index.html, which is shown in Figure 3.1.

FIGURE 3.1 A first Ajax application

images

This application boasts a button with the caption “Fetch the Message.” When you click that button, the application uses Ajax techniques to download text behind the scenes and display it.

The data that the application reads from the server is stored in a file named data.txt. Here are the contents of that file:

This text was fetched from the server with Ajax.

When you click the button in this application, the application downloads this text ...

Get Ajax Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.