jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] ) Returns: jqXHR
Description: Load JSON-encoded data from the server using a GET HTTP request.
-
version added: 1.0jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] )
-
urlType: StringA string containing the URL to which the request is sent.
-
dataType: PlainObjectA plain object or string that is sent to the server with the request.
-
success(data, textStatus, jqXHR)Type: Function()A callback function that is executed if the request succeeds.
-
This is a shorthand Ajax function, which is equivalent to:
1
2
3
4
5
6
|
|
Data that is sent to the server is appended to the URL as a query string. If the value of the data
parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL.
Most implementations will specify a success handler:
1
2
3
4
5
6
7
8
9
10
11
12
|
|
This example, of course, relies on the structure of the JSON file:
1
2
3
4
5
|
|
Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.
The success
callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON()
method. It is also passed the text status of the response.
As of jQuery 1.5, the success
callback function receives a "jqXHR" object (in jQuery 1.4, it received the XMLHttpRequest
object). However, since JSONP and cross-domain GET requests do not use XHR, in those cases the jqXHR
and textStatus
parameters passed to the success callback are undefined.
Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.
JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp
data type in $.ajax()
for more details.
The jqXHR Object
As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest
object. This jQuery XHR object, or "jqXHR," returned by $.getJSON()
implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see Deferred object for more information). For convenience and consistency with the callback names used by $.ajax()
, it provides .error()
, .success()
, and .complete()
methods. These methods take a function argument that is called when the request terminates, and the function receives the same arguments as the correspondingly-named $.ajax()
callback.
The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including $.getJSON()
, to chain multiple .success()
, .complete()
, and .error()
callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|
Additional Notes:
- Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
- Script and JSONP requests are not subject to the same origin policy restrictions.
Examples:
Example: Loads the four most recent cat pictures from the Flickr JSONP API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
|
Example: Load the JSON data from test.js and access a name from the returned JSON data.
1
2
3
|
|
Example: Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data.
1
2
3
|
|