Skip to content

Commit 802d9db

Browse files
committed
$.getJSON: add deprecation notice for jqXHR.success(), et al
* Also, update examples per style guide * Closes #270
1 parent fce72bc commit 802d9db

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

entries/jQuery.getJSON.xml

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,55 +54,68 @@
5454
<h4 id="jsonp">JSONP</h4>
5555
<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="http://api.jquery.com/jQuery.ajax/">$.ajax()</a></code> for more details.</p>
5656
<h4 id="jqxhr-object">The jqXHR Object</h4>
57-
<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="http://api.jquery.com/category/deferred-object/">Deferred object</a> for more information). For convenience and consistency with the callback names used by <code><a href="http://api.jquery.com/jQuery.ajax/">$.ajax()</a></code>, it provides <code>.error()</code>, <code>.success()</code>, and <code>.complete()</code> 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 <code>$.ajax()</code> callback.</p>
58-
<p>The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including <code>$.getJSON()</code>, to chain multiple <code>.success()</code>, <code>.complete()</code>, and <code>.error()</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>
57+
<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) 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 $.ajax() documentation.</p>
58+
<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>
5959
<pre><code>// Assign handlers immediately after making the request,
6060
// and remember the jqxhr object for this request
61-
var jqxhr = $.getJSON("example.json", function() {
62-
alert("success");
61+
var jqxhr = $.getJSON( "example.json", function() {
62+
console.log( "success" );
6363
})
64-
.success(function() { alert("second success"); })
65-
.error(function() { alert("error"); })
66-
.complete(function() { alert("complete"); });
64+
.done(function() { console.log( "second success" ); })
65+
.fail(function() { console.log( "error" ); })
66+
.always(function() { console.log( "complete" ); });
6767

6868
// perform other work here ...
6969

7070
// Set another completion function for the request above
71-
jqxhr.complete(function(){ alert("second complete"); });</code></pre>
71+
jqxhr.complete(function() { console.log( "second complete" ); });</code></pre>
72+
<h4>Deprecation Notice</h4>
73+
<p>The <code>jqXHR.success()</code>, <code>jqXHR.error()</code>, and <code>jqXHR.complete()</code> callback methods introduced in jQuery 1.5 are <strong>deprecated as of jQuery 1.8</strong>. To prepare your code for their eventual removal, use <code>jqXHR.done()</code>, <code>jqXHR.fail()</code>, and <code>jqXHR.always()</code> instead.</p>
7274
</longdesc>
7375
<note id="same-origin-policy" type="additional"/>
7476
<note id="same-origin-policy-exceptions" type="additional"/>
7577
<example>
76-
<desc>Loads the four most recent cat pictures from the Flickr JSONP API.</desc>
78+
<desc>Loads the four most recent pictures of Mount Rainier from the Flickr JSONP API.</desc>
7779
<code><![CDATA[
78-
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
79-
{
80+
(function() {
81+
var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
82+
$.getJSON( flickerAPI, {
8083
tags: "mount rainier",
8184
tagmode: "any",
8285
format: "json"
83-
},
84-
function(data) {
85-
$.each(data.items, function(i,item){
86-
$("<img/>").attr("src", item.media.m).appendTo("#images");
87-
if ( i == 3 ) return false;
86+
})
87+
.done(function( data ) {
88+
$.each( data.items, function( i, item ) {
89+
$( "<img/>" ).attr( "src", item.media.m ).appendTo( "#images" );
90+
if ( i === 3 ) {
91+
return false;
92+
}
8893
});
89-
});]]></code>
94+
});
95+
})();
96+
]]></code>
9097
<html><![CDATA[<div id="images">
9198
9299
</div>]]></html>
93100
<css><![CDATA[img{ height: 100px; float: left; }]]></css>
94101
</example>
95102
<example>
96103
<desc>Load the JSON data from test.js and access a name from the returned JSON data.</desc>
97-
<code><![CDATA[$.getJSON("test.js", function(json) {
98-
alert("JSON Data: " + json.users[3].name);
104+
<code><![CDATA[$.getJSON( "test.js", function( json ) {
105+
console.log( "JSON Data: " + json.users[3].name );
99106
});]]></code>
100107
</example>
101108
<example>
102-
<desc>Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data.</desc>
103-
<code><![CDATA[$.getJSON("test.js", { name: "John", time: "2pm" }, function(json) {
104-
alert("JSON Data: " + json.users[3].name);
105-
});]]></code>
109+
<desc>Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data.
110+
If an error occurs, log an error message instead.</desc>
111+
<code><![CDATA[$.getJSON( "test.js", { name: "John", time: "2pm" } )
112+
.done(function( json ) {
113+
console.log( "JSON Data: " + json.users[3].name );
114+
})
115+
.fail(function( jqxhr, textStatus, error ) {
116+
var err = textStatus + ', ' + error;
117+
console.log( "Request Failed: " + err);
118+
});]]></code>
106119
</example>
107120
<category slug="ajax/shorthand-methods"/>
108121
<category slug="version/1.0"/>

0 commit comments

Comments
 (0)