Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AJAX Basics (retiring) Programming AJAX Introducing JSON

Rob McClara
Rob McClara
19,257 Points

A web server 'response' is returned in the form of text. How do we know what format the web server is using?

I understand the concepts of structured data formats like JSON and XML. However, I'm confused on how you know what format, if any, the web server will return.

Let's say I am wanting a response formatted in JSON from the web server. If the 'response' is in another format, it will not work.

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

You don't need to worry about this really. There's a very simple method of sorting out how the server handles the request which is called "Parsing".

What this does is converts the information into a format that will work for you.

var employees = JSON.parse(xhr.responseText);

In this example, the method is stored in a variable. and the responseText (the information sent back) is converted from plainText to something AJAX friendly. So all the legwork is done for you.

Hope this helps. :)

Diego ROJAS
Diego ROJAS
1,781 Points

When a server writes its response they can fill in options such as Content-Type which can be a lot of different things like

res.writeHead(200, {"Content-Type": "text/javascript"});

or

res.writeHead(200, {"Content-Type": "text/plain"});

and

res.writeHead(200, {"Content-Type": "application/json"});