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 trialAaron Smith
19,091 PointsHow do I know when to use $.get() and when to use $.getJSON()?
How do I know when to use $.get() and when to use $.getJSON()? Do I need to know in advance how the data from the server will be formatted? What happens if I use the wrong function?
1 Answer
Alexander Acker
18,123 PointsTo utilize the .getJSON() method you will certainly know that you'll need to be utilizing a JSON file as a data type otherwise the method will fail if you're trying to work with say XML for example. From what I've seen a lot of API documentation will explain what kind of data will be returned during a GET request thus if JSON is what's returned it's up to you utilize either options, as well as the .ajax shorthand if you'd like, in your code.
If you're worried about the wrong function you can always setup a chained .fail() method which you can configure to console out and error or alert you if something went wrong. Highly recommend going through the API documentation on both of these methods for specific examples and clarity.
http://api.jquery.com/jQuery.getJSON/ http://api.jquery.com/jQuery.get/