26
Chapter 1
Initializing the connection
You’ve got a request object, and a variable with the URL to connect to.
Now you just need to tell the request object how to connect to the server,
and give it that URL as the address to connect to. Here’s how you do that:
Q:
I thought that it was better to use
“POST” for web page requests.
A: POST is usually used when you are
sending a server lots of data, or are completing
a form submit that involves money, or placing
an order. Since we’re not sending any data to
the PHP script, and there’s no purchase being
made here. it’s easier to use a GET request;
we’ll look at POST requests a little later on.
Q:
Should I ever use open() to make a
synchronous request, and set that last value
to “false”?
A: Sure. Sometimes you don’t want the user
to be able to keep using your page while the
server is answering a request. For instance, if
a user is placing an order, you probably don’t
want to let them do anything else until you
confirm that their order was accepted.
questions
Frequently asked
?
Let’s break that down a bit...
request.open(
“GET”,
url,
true);
The open()
method initializes
a connection...
...and then “GET” indicates how
to send data to the server.
This is the URL for the
PHP script running on
Katie’s web server.
This means that the request should
be asynchronous... in other words,
this lets JavaScript know that it
doesn’t need to wait around for a
resp