jQuery.getJSON(url [, data] [, success]) (since 1.0) is a GET shorthand for loading JSON. It is equivalent to $.ajax({ dataType: "json", url, data, success }). The success callback receives a parsed object or array — no manual JSON.parse().
Syntax
Start with these common calls. Keep them short and readable.
userId = 1 → GET /posts?userId=1
success receives a JSON array
#result shows count + first titles
How It Works
Read input. Take userId from the form.
Encode. jQuery turns { userId } into ?userId=….
Parse. Success gets an array of matching posts.
Frequently Asked Questions
jQuery.getJSON() is a shorthand for $.ajax() configured as a GET with dataType "json". Pass a URL, optional data, and optional success callback — jQuery parses the response into a JavaScript object or array and returns a jqXHR Promise.
They are functionally the same. $.getJSON() presets dataType to "json"; $.get() takes "json" as a fourth argument. Prefer $.getJSON() when you always expect JSON — clearer intent, fewer arguments.
$.getJSON() expands to $.ajax({ url, data, success, dataType: "json" }). Use getJSON for a simple JSON GET. Use $.ajax() when you need headers, beforeSend, POST, global: false, or other full settings.
If the URL includes "callback=?" (or a similar server placeholder), jQuery treats the request as JSONP. That bypasses same-origin limits via a script tag. For JSONP, jqXHR and textStatus in the success callback may be undefined.
Yes — since jQuery 1.4, a JSON syntax error usually fails silently: success never runs unless you chain .fail(), use a global ajaxError handler, or handle errors another way. Always chain .fail() for user-facing requests.
A jqXHR object that implements jQuery’s Promise interface. Chain .done() for success, .fail() for errors, and .always() for either outcome. You can attach handlers after the request finishes; they still fire.