Eight Weeks of Prototype: Week 5, Ajax with Prototype
There are three different classes Prototype provides to perform Ajax requests. These are
This is the primary class that is used for Ajax, which the other two classes mentioned above use also.
Ajax.Request provides a cross-browser solution for performing Ajax requests without requiring that you know how to specifically use
This class is simple to use – just instantiate it and pass to it the URL you want to send a HTTP request to as the first argument. You can optionally pass a second argument to the constructor which you can use to specify a number of different options. It's pretty rare that you would ever use this class without specifying the second argument, since most of the time you will want to use the response in some way.
Ajax.PeriodicalUpdater classes we will look at shortly.
Ajax.Requestis a class, not a function, so you must remember to instantiate the class by including the new keyword.
In the above example, the
options object is used to hold the various request options. This may include get or post data to include in the request or instructions on what to do on successful completion (or failure) of the request. In this example, I have set the request method to be
post, simply by specifying the
In this example, once the script is requested nothing will happen since we haven't defined any callbacks for handling the response. I will cover this in the next section.