-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathjQuery.getJSON.xml
149 lines (146 loc) · 7.37 KB
/
jQuery.getJSON.xml
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0"?>
<entry type="method" name="jQuery.getJSON" return="jqXHR">
<title>jQuery.getJSON()</title>
<signature>
<added>1.0</added>
<argument name="url" type="String">
<desc>A string containing the URL to which the request is sent.</desc>
</argument>
<argument name="data" optional="true">
<type name="PlainObject"/>
<type name="String"/>
<desc>A plain object or string that is sent to the server with the request.</desc>
</argument>
<argument name="success" optional="true" type="Function">
<argument name="data" type="PlainObject" />
<argument name="textStatus" type="String"/>
<argument name="jqXHR" type="jqXHR"/>
<desc>A callback function that is executed if the request succeeds.</desc>
</argument>
</signature>
<desc>Load JSON-encoded data from the server using a GET HTTP request.</desc>
<longdesc>
<p>This is a shorthand Ajax function, which is equivalent to:</p>
<pre><code>
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
</code></pre>
<p>Data that is sent to the server is appended to the URL as a query string. If the value of the <code>data</code> parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL.</p>
<p>Most implementations will specify a success handler:</p>
<pre><code>
$.getJSON( "ajax/test.json", function( data ) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
</code></pre>
<p>This example, of course, relies on the structure of the JSON file:</p>
<pre><code>
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
</code></pre>
<p>Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.</p>
<p>The <code>success</code> callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the <code><a href="/jQuery.parseJSON/">$.parseJSON()</a></code> method. It is also passed the text status of the response.</p>
<p><strong>As of jQuery 1.5</strong>, the <code>success</code> callback function receives a <a href="/jQuery.get/#jqxhr-object">"jqXHR" object</a> (in <strong>jQuery 1.4</strong>, it received the <code>XMLHttpRequest</code> object). However, since JSONP and cross-domain GET requests do not use <abbr title="XMLHTTPRequest">XHR</abbr>, in those cases the <code>jqXHR</code> and <code>textStatus</code> parameters passed to the success callback are undefined.</p>
<div class="warning">
<p><strong>Important:</strong> 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 <a href="https://json.org/">https://json.org/</a>.</p>
</div>
<h4 id="jsonp">JSONP</h4>
<p>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 <code>jsonp</code> data type in <code><a href="/jQuery.ajax/">$.ajax()</a></code> for more details.</p>
<h4 id="jqxhr-object">The jqXHR Object</h4>
<p><strong>As of jQuery 1.5</strong>, all of jQuery's Ajax methods return a superset of the <code>XMLHTTPRequest</code> object. This jQuery XHR object, or "jqXHR," returned by <code>$.getJSON()</code> implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see <a href="/category/deferred-object/">Deferred object</a> for more information). The <code>jqXHR.done()</code> (for success), <code>jqXHR.fail()</code> (for error), and <code>jqXHR.always()</code> (for completion, whether success or error; added in jQuery 1.6) methods take a function argument that is called when the request terminates. For information about the arguments this function receives, see the <a href="/jQuery.ajax/#jqXHR">jqXHR Object</a> section of the <code>$.ajax()</code> documentation.</p>
<p>The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including <code>$.getJSON()</code>, to chain multiple <code>.done()</code>, <code>.always()</code>, and <code>.fail()</code> 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.</p>
<pre><code>
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "example.json", function() {
console.log( "success" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
console.log( "second complete" );
});
</code></pre>
<h4>Deprecation Notice</h4>
<p>The <code>jqXHR.success()</code>, <code>jqXHR.error()</code>, and <code>jqXHR.complete()</code> callback methods are <strong>removed as of jQuery 3.0</strong>. You can use <code>jqXHR.done()</code>, <code>jqXHR.fail()</code>, and <code>jqXHR.always()</code> instead.</p>
</longdesc>
<note id="same-origin-policy" type="additional"/>
<note id="same-origin-policy-exceptions" type="additional"/>
<example>
<desc>Loads the four most recent pictures of Mount Rainier from the Flickr JSONP API.</desc>
<code><![CDATA[
(function() {
var flickerAPI = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON( flickerAPI, {
tags: "mount rainier",
tagmode: "any",
format: "json"
})
.done(function( data ) {
$.each( data.items, function( i, item ) {
$( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
if ( i === 3 ) {
return false;
}
});
});
})();
]]></code>
<html><![CDATA[
<div id="images"></div>
]]></html>
<css><![CDATA[
img {
height: 100px;
float: left;
}
]]></css>
</example>
<example>
<desc>Load the JSON data from test.js and access a name from the returned JSON data.</desc>
<code><![CDATA[
$.getJSON( "test.js", function( json ) {
console.log( "JSON Data: " + json.users[ 3 ].name );
});
]]></code>
</example>
<example>
<desc>Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data.
If an error occurs, log an error message instead.</desc>
<code><![CDATA[
$.getJSON( "test.js", { name: "John", time: "2pm" } )
.done(function( json ) {
console.log( "JSON Data: " + json.users[ 3 ].name );
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
]]></code>
</example>
<category slug="ajax/shorthand-methods"/>
<category slug="version/1.0"/>
<category slug="version/1.5"/>
</entry>