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

David Roman
David Roman
6,550 Points

From the methods explained for accessing resources from other domains. Where does REST APIs fit in?

The video discusses the way you can use AJAX to access external resources. In which method does REST APIs fit in?

1 Answer

Matt F.
Matt F.
9,518 Points

Hi David,

REST is just one type of API.

When you connect to an API, you are essentially just giving instructions to code running on a server somewhere. When you execute:

$.ajax({
  url: "exampleSite/news",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

The code running on a server somewhere might do something like this:


Function that runs whenever user connects to /news

GET request

Any query parameters? Nope, so we will search for all news stories and use the default of 10 stories to return, with the latest stories being the ones the client wants

Hey database, it is the server, can I have the latest 10 stories in the news database?

Sure server, here is the data that you wanted.

Sending the news data as the response to the AJAX request


After that, the browser will receive the data, and the even loop will call the AJAX call's done callable.