9

Right from the start i want to mention that i know THIS question has the same title as mine but that user asked a question regarding a problem he encountered.

My question about the differences is more subjective. I'm learning to use Jquery and Ajax and i came across both methods. Now to me they both seem to do the same. (get raw JSON data from a URL specified) but i'm sure there is a bigger difference.

I also noticed that people tend to use $.ajax more then getJSON, is there a reason for that aswell?

Any help is appreciated!

Community
  • 1
  • 1
Marco Geertsma
  • 803
  • 2
  • 9
  • 28
  • 3
    `jQuery.getJSON()` just a short hand for `jQuery.ajax()` http://api.jquery.com/jquery.getjson/ What else you want to know? – Satpal Jan 15 '16 at 08:30
  • @Satpal So people use .ajax more because it gets more data? i'm puzzled as of why you would use getJSON in the first place then? – Marco Geertsma Jan 15 '16 at 08:33
  • 1
    I would only use getJSON when I know dataType is JSON only and I don't need to specify headers, converts etc – Satpal Jan 15 '16 at 08:37
  • But do you use getJSON then or .ajax with datatype JSON? See where my question lies? I know that they are pretty simalar now but now i dont see why there is a getJSON to begin with. – Marco Geertsma Jan 15 '16 at 08:38
  • You are not getting the point, Why you use short hand? because Its easy to use for specific scenario – Satpal Jan 15 '16 at 08:39
  • If you are expecting JSON, use $.getJSON. It saves you the trouble of typing the more extensive $.ajax and is more readable. Plus if no JSON is returned, you won't get a response to work with, which saves you the trouble of checking if what was returned is actually JSON. – minitauros Jan 15 '16 at 09:04

1 Answers1

14

$.getJSON()

From http://api.jquery.com/jquery.getjson/

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

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

$.ajax()

From http://api.jquery.com/jquery.ajax/

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

That says that if you set dataType to JSON and if no JSON is returned, a parse error is thrown.

So judging from the docs, $.getJSON() is equal to $.ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error.

So you were mostly right about the two being pretty much the same :). $.getJSON() is just shorthand for the more extensive $.ajax().

minitauros
  • 1,920
  • 18
  • 20