$.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()
.