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) AJAX and APIs Displaying the Photos

Carie Pigeon
Carie Pigeon
7,892 Points

Flickr API Code Question

In the Flickr API project in the AJAX course the instructor uses function displayPhotos(data) { I see that earlier we defined our wanted search to be var animal = $(this).text(), which grabs the keyword from the button element that is clicked on. Then we define var flickrOptions = { tags: animal, format: "json" }; I understand all of this. But then we go on to code: function displayPhotos(data)... I'm not sure what the "data" argument is, it hasn't been explicitly defined in the code beforehand. Is the "data" argument pointing to the json file received? Thanks!

1 Answer

andren
andren
28,558 Points

Strictly speaking data is a parameter, not an argument. Parameters are variables whose value is set by the argument that is passed in when the function is called. So the data parameter will contain whatever argument the displayPhotos function is called with. So the real question is where is the function called?

Well the answer is that it is called by jQuery itself because you pass the function into the getJSON jQuery method:

$.getJSON(flickrAPI, flickrOptions, displayPhotos)

The getJSON method takes three arguments. The first is the URL where the request should be sent, the second is the data that should be sent to the URL, and the third is a callback function which will be called when the request succeeds.

By looking at the documentation of the getJSON method (linked above) you can see that the first argument it passes into the function (and therefore the one the data parameter is set to) is the data that it received from the request. Which will indeed be the JSON that flickr sends in response.