HTML and CSS Reference
Whether retrieving a simple text file, XML file, sending form data, or retrieving infor-
mation from a database, a request will ask for a resource from the server. To get this
information the request object needs to know how the request will be submitted (i.e.,
GET or POST; use capital letters) and the URL defining the location of the file or server-
side script. (When creating HTML forms, this information was normally stored in the
ACTION and METHOD attributes of the <form> tag.) Once the object has this informa-
tion, the request can be made.
The open() Method. The open() method prepares or initializes the object for com-
munication with the server, whereas the send() method actually makes the request. The
open() method takes three arguments:
1. The request type; that is, how the data is submitted, either GET or POST. 2
2. The URL of the file or server-side program and parameters representing the data
being sent as key/value pairs.
3. An optional third parameter of Boolean true (asynchronous) or false (synchro-
nous). When set to true , the default, the processing will continue without wait-
ing for the server to respond, whereas with a false setting, the processing will
stop until the server responds.
Except for simple file retrieval or searches, the POST method is used, but this requires
an additional request header method, and the data represented as name/value pairs is
sent to the server as an argument to the send() method.
It is important to note that for security reasons, the URL must be within the same
HttpRequest to any server other than the same Web server currently being visited by the
EXAMPLE (S AMPLE C ODE )
objectRequest.open("GET", "myfile.txt", true);
GET or POST. The way data is sent to a server with the HTTP protocol depends on
whether the method being used is GET or POST. (To review these two methods, go to
Chapter 11, section “About HTML Forms” on page 334.) In either case, the data being
sent is in a URL-encoded query string consisting of name/value pairs. With the GET
method, the browser sends data in the URL (see Figure 18.3), whereas with the POST
method it sends additional data to the server specified in headers, a blank line, and
finally the data (see Figure 18.4). Ajax handles GET and POST differently as well.
2. You can also use the HEAD request method.