Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, you'll learn which formats web servers commonly send as a response to an AJAX request: plain text, HTML, and XML. Learn the basics of XML.
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
After you send an AJAX request, your
callback function waits for a response.
0:00
Web servers usually reply to AJAX requests
with a text response.
0:05
This could be a simple message, like okay,
or message received.
0:10
For instance, say you're using AJAX to
submit information from a form.
0:14
A user visits the web page, enters
information, and hits a submit button.
0:19
Instead of exiting the page, as you
normally do when you submit a
0:24
form, you could use AJAX to send the
form's information to the server.
0:26
After the server gets that data and does
something with it, like, you know,
0:31
adding it to a database, the web server
could send a simple text response.
0:34
Form submitted successfully, for example.
0:38
If the server was able to process the form
or if
0:41
there was an error, it could send the
message, error submitting form.
0:44
Using this information, you could tell the
user that the form went through
0:48
just fine, or there was an error and they
need to try again.
0:51
You can also use AJAX to request a static
0:55
file, meaning a file just sitting on your
web server.
0:57
This is what we did in the simple project
earlier in this stage.
1:00
We loaded a file containing a bit of HTML
and displayed it on a web page.
1:04
It's easy to add a chunk of HTML to a web
page or to
1:09
write JavaScript to respond to a simple,
one word text message from the web server.
1:12
However, sometimes you get a lot of
information from the server.
1:17
Perhaps 100 search results, 50 tweets, or
a long list of company employees.
1:20
For dealing with lots of data, it's a good
idea to have a structured data format.
1:26
By structured, I mean data that
1:31
is ordered consistently, has identifiers
that indicate
1:33
what the data is, and is easy for
JavaScript to analyze and use.
1:36
Two common data interchange formats are
XML and JSON.
1:41
XML is the X in AJAX.
1:46
XML stands for extensible markup language.
1:48
It's very similar to HTML, and like HTML,
XML uses tags to structure data.
1:51
For example, say you wanted to store a
1:58
list of contacts with names and phone
numbers.
1:59
You could start with a tag named contacts.
2:03
Inside that tag, you could add individual
contacts using another tag called contact.
2:06
In HTML, we have tags like head, title,
body, div, and
2:12
ul, but XML doesn't limit you to just a
handful of tags.
2:16
You can create your own tags.
2:21
In fact, there is no official contact tag
like you see here.
2:23
I just made it up for this lesson.
2:27
And you can make up your own XML tags when
creating XML documents.
2:29
Let's keep building this XML file.
2:34
To add a name and a phone number for one
2:36
contact, we'd nest new tags inside the
contact tag like this.
2:38
[SOUND] You can add more contacts by
adding additional contact tags.
2:43
The benefit of a structured format like
XML is that the data is clearly and
2:50
consistently organized, making it easy for
a computer
2:54
to split the data up into usable chunks.
2:58
The process of breaking a file up into
easily accessed parts is called parsing.
3:01
XML is a very common format for exchanging
data between computers.
3:06
Most server-side languages handle XML
easily.
3:11
However, using XML data with JavaScript
isn't so easy.
3:14
It involves several steps, including
analyzing
3:18
or parsing the XML document, then
3:21
going through each of its nodes to extract
data from the tags.
3:23
For many AJAX applications, there's a
3:27
better, more JavaScript-like data format
called JSON.
3:29
In fact, JSON has become the most popular
way to exchange data using AJAX.
3:33
Because of that, we'll spend a lot of time
on it in the next stage.
3:39
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