JavaScript AJAX Basics 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.