|
54 | 54 | <h4 id="jsonp">JSONP</h4> |
55 | 55 | <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> |
56 | 56 | <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> |
59 | 59 | <pre><code>// Assign handlers immediately after making the request, |
60 | 60 | // 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" ); |
63 | 63 | }) |
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" ); }); |
67 | 67 |
|
68 | 68 | // perform other work here ... |
69 | 69 |
|
70 | 70 | // 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> |
72 | 74 | </longdesc> |
73 | 75 | <note id="same-origin-policy" type="additional"/> |
74 | 76 | <note id="same-origin-policy-exceptions" type="additional"/> |
75 | 77 | <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> |
77 | 79 | <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, { |
80 | 83 | tags: "mount rainier", |
81 | 84 | tagmode: "any", |
82 | 85 | 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 | + } |
88 | 93 | }); |
89 | | - });]]></code> |
| 94 | + }); |
| 95 | +})(); |
| 96 | +]]></code> |
90 | 97 | <html><![CDATA[<div id="images"> |
91 | 98 |
|
92 | 99 | </div>]]></html> |
93 | 100 | <css><![CDATA[img{ height: 100px; float: left; }]]></css> |
94 | 101 | </example> |
95 | 102 | <example> |
96 | 103 | <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 ); |
99 | 106 | });]]></code> |
100 | 107 | </example> |
101 | 108 | <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> |
106 | 119 | </example> |
107 | 120 | <category slug="ajax/shorthand-methods"/> |
108 | 121 | <category slug="version/1.0"/> |
|
0 commit comments