Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.