Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed AJAX Basics!
You have completed AJAX Basics!
Preview
Learn about the two most common methods for sending requests to a web server. What is the difference between the two and when to use GET, when to use POST?
Key Points
There are two common methods for sending HTTP requests:
- GET. Used for most requests. Browser uses the GET method whenever it requests a new web page, CSS file, image, and so on. Use GET when you want to "get" something from the server.
- POST. Used frequently with web forms to send data to store in a database. Use POST when sending data that will store, delete or update information from a database.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Ajax isn't that hard.
0:00
We've already looked at the basic steps
required
0:01
to set up and send an Ajax request.
0:04
First you create the XMLHttpRequest
object, then you define
0:07
a callback function, you open the request,
and then you send it.
0:11
As I mentioned in the last video opening a
request requires specifying the method
0:16
you'll use to send the request and the URL
you'll send the request to.
0:21
The two most common methods are GET and POST.
0:25
Here's an easy way to think about which
method you should use.
0:29
Use GET when you're only interested in
0:32
receiving or getting information from a
server.
0:34
Such as the result of a database search or
retrieving a set of tweets.
0:37
Use post when sending data that's going to
be saved
0:43
like an email address for an email
newsletter sign-up form.
0:45
Or a vote for a Reddit post.
0:49
You use the GET method all the time when
you
0:52
open a web browser and type in a web
address.
0:54
The web browser uses a get to request a
webpage.
0:57
The GET method is meant to get a resource,
like
1:00
a webpage, image, or other file from a web
server.
1:03
In other words, all that's required for a
get request is a url.
1:07
But with AJAX, we frequently want more
than just a static file of text or HTML.
1:12
We're after dynamic, that is, changing
information.
1:17
Like the latest tweets from your friends,
the results of
1:20
a web search, or information on an
employee in a business.
1:23
For a server site program to send
1:27
you customized information, like the map
of your
1:29
current location or photos from your flickr
account,
1:32
you need to send the server some
information.
1:35
You can do this by adding data to the url
sent to the server.
1:38
You may have seen urls that look something
like this.
1:43
The part after the question mark is called
a query string.
1:47
A query string appears at the end of a URL
and provides a way to
1:51
send additional information that a web
server can
1:54
use to control the output of its
response.
1:57
Commonly, the data in a query string is
used to search a database
2:01
of information and return just a single
record or a small subset of information.
2:05
In this case, the file employees.php
2:11
is programmed using the PHP programming
language.
2:14
In other words, it's not a regular old
HTML file, it's a program
2:18
that does something and it requires
information so it knows what to do.
2:22
Its purpose might be to retrieve
information on employees at the company.
2:26
This information is often sent in a query
string.
2:30
A query string is made up of one or more
name value pairs.
2:34
In this example the query string contains
one name value pair.
2:38
lastName equals Jones.
2:42
lastName is the name or property.
2:45
The equal sign assigns a value to that
property.
2:47
Jones is the value here.
2:51
Query strings can hold multiple name value
pairs.
2:53
Simply add an ampersand, followed by
another
2:56
name, an equal sign and another value.
2:59
There are some requirements for query
strings.
3:01
Most importantly, you can't just use any
symbol you want in a query string.
3:05
Some characters like the ampersand, equal,
space, and
3:10
quote marks have special meaning in a URL.
3:13
So if you send information that includes
3:17
those special characters you need to
encode them.
3:19
That is, translate them into a set of
symbols that
3:22
are safe and don't conflict with their URL
specific version.
3:25
For example, ampersand is represented by
%26.
3:29
A space is converted to a plus.
3:33
And a plus is converted to %2B.
3:37
There
3:40
are lots of online tools that demonstrate
what URL encoded strings look like.
3:42
The site URL Encode and Decode online is
one of them.
3:47
For example, let me type in a string with
spaces, quotes, and an ampersand.
3:51
[BLANK_AUDIO]
3:55
Now, when I click the URL encode button,
you can see how it's translated.
4:01
You can also use this tool to decode a URL
encoded string.
4:07
Don't worry about memorizing these codes.
4:11
As you'll learn later, JavaScript and
jQuery both
4:13
include functions that do this conversion
for you.
4:16
The GET method is a simple way to send
data to a web server.
4:19
However, there are some down sides to it.
4:22
First, all of the data is sent in the URL,
which means that if you need
4:25
to send sensitive information, like a
social security
4:29
number, a password or your personal
telephone number.
4:32
It will appear in the computer's browser
history
4:35
and show up in the web server's log files.
4:38
This isn't the most secure solution.
4:41
Second, there's only so much information
you can put into a URL.
4:43
For example, Internet Explorer can only
handle URLs that are 2083 characters long.
4:47
When you need to send lots of information,
4:54
like a blog post, the get method isn't
good.
4:56
That's why http offers another method
called POST.
4:59
The POST method sends data differently
than get.
5:03
Whereas get puts all its information in
the url, the post method sends
5:06
data separate from the url, in what's
called the body of the request.
5:11
For example, say you had a sign up
5:15
form on your site with username and
password fields.
5:17
Using get to submit that form, you'd see
5:21
the username and password in the URL like
this.
5:23
However, using post, the URL to the sign
up script has no extra information.
5:28
The actual form data is sent separate from
the URL.
5:34
Not only is this more secure, it also
let's
5:37
you send a lot more information to the
server.
5:40
The post method does require special
encoding, just like get requests.
5:42
In addition, you need to set up a special
header for the request.
5:47
That's an instruction, sent to the server,
telling
5:52
it what kind of data it should expect.
5:54
In many cases, you can use either get or
post, but
5:58
the basic rule of thumb is that if you're
requesting information,
6:00
like search results, new HTML to add to a
page,
6:04
photos from a photo service and so on, use
get.
6:07
On the other hand, use post if you're
6:11
sending information that's to be stored in
a database,
6:13
if the data is sensitive, like a password,
or
6:16
if you're sending lots of information from
a form.
6:19
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up