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 trialDavid Roman
6,550 PointsFrom the mentioned methods to access external resources from other domains. Where does REST APIs fit in?
This video analyzes a couple of ways to access external resources. In which of them does REST APIs fit in?
2 Answers
Matt F.
9,518 PointsHi 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.
PoJung Chen
5,856 PointsMatt F. I have the same problem after watching this lecture. The url you provided could not be found anymore. Is there other resource that can help me understand the concept?
David Roman
6,550 PointsDavid Roman
6,550 PointsHi Matt,
Thank you for your answer. I have a question about this matter. In the case of connecting to an API from an external domain (external resource), wouldn't there be a problem sending the ajax request and getting the response back given the same-origin domain restriction?
Matt F.
9,518 PointsMatt F.
9,518 PointsThis thread on Stack Overflow might help:
http://stackoverflow.com/questions/3536800/same-origin-policy-ajax-using-public-apis
David Roman
6,550 PointsDavid Roman
6,550 PointsHi Matt,
Thank you for your response. Now, I have a better understanding of how to get data from another domain using a REST API and AJAX.