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 Making the AJAX Request

shobha gunupuru
shobha gunupuru
1,137 Points

how to write the code when there is api_key given

how to write the getJson method when api key has to be used

3 Answers

You would just pass api_key as another parameter/option. Getting that API key may be a longer process though:

User Authentication

shobha gunupuru
shobha gunupuru
1,137 Points

how to pass api_key as another parameter or option. can you please explain with a code sample

I just mashed the keyboard, so that's not a real API key of course, but it'd more or less be this:

var flickrOptions = {
      api_key: "askf92noskvsijdfpjweoihfg8435nwdsfnsdf",
      tags: query,
      format: "json"
    };

A full example would require looking at the structure of the response and changing how you're using it in the output HTML, etc. And would probably need other parameters/arguments/options passed to the API.

shobha gunupuru
shobha gunupuru
1,137 Points

'''$.ajax({ type :'GET', url :'http://hire.lcdevelopment.com/api/users api_key=gunupuru.shobha@gmail.com',

    });

those are url and apikey given how do i write the code now

Have a look at the jQuery documentation for $.getJSON.

Basically, the pattern is you pass the url, data and success callback function. In the Flickr API example, these corresponded to variables flickrAPI, flickrOptions, and displayPhotos:

$.getJSON(flickrAPI, flickrOptions, displayPhotos);

Rename the variables to something suiting your needs, and replace as necessary (your url above could replace the flickrAPI variable, for example).

Have a try, see how you go!