Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 91,253 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"});