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) jQuery and AJAX The Office Status Project Revisited

Avishek Roy
Avishek Roy
2,935 Points

Difference between .get and .getJSON

What is the difference between .get and .getJSON method?

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Avishek,

Both .get and .getJSON are shorthand syntax for the jQuery .ajax function. What makes them different is the arguments that are passed to the call behind the scenes.

For example, .get is more generic than .getJSON, so you should pass an argument specifying what datatype to accept back from the call, where as .getJSON will automatically set the dataType to 'json'.

Here's an excerpt from the jQuery documentation on .get and .getJSON.

.get

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

.getJSON

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

I hope this helps.