@@ -4,8 +4,6 @@ section : 3
44title : jQuery's Ajax-Related Methods
55attribution : jQuery Fundamentals
66---
7- ## jQuery's Ajax-Related Methods
8-
97While jQuery does offer many Ajax-related convenience methods, the core
108` $.ajax ` method is at the heart of all of them, and understanding it is
119imperative. We'll review it first, and then touch briefly on the convenience
@@ -27,44 +25,42 @@ documentation of the configuration options, visit
2725[ http://api.jquery.com/jQuery.ajax/ ] (http://api.jquery.com/jQuery.ajax/ "$.ajax
2826documentation on api.jquery.com").
2927
30- <div class =" example " markdown =" 1 " >
31- Using the core ` $.ajax ` method
32-
33- $.ajax({
34- // the URL for the request
35- url : 'post.php',
36-
37- // the data to send
38- // (will be converted to a query string)
39- data : { id : 123 },
40-
41- // whether this is a POST or GET request
42- type : 'GET',
43-
44- // the type of data we expect back
45- dataType : 'json',
46-
47- // code to run if the request succeeds;
48- // the response is passed to the function
49- success : function(json) {
50- $('<h1/>').text(json.title).appendTo('body');
51- $('<div class="content"/>')
52- .html(json.html).appendTo('body');
53- },
54-
55- // code to run if the request fails;
56- // the raw request and status codes are
57- // passed to the function
58- error : function(xhr, status) {
59- alert('Sorry, there was a problem!');
60- },
61-
62- // code to run regardless of success or failure
63- complete : function(xhr, status) {
64- alert('The request is complete!');
65- }
66- });
67- </div >
28+ <javascript caption =" Using the core `$.ajax` method " >
29+ $.ajax({
30+ // the URL for the request
31+ url : 'post.php',
32+
33+ // the data to send
34+ // (will be converted to a query string)
35+ data : { id : 123 },
36+
37+ // whether this is a POST or GET request
38+ type : 'GET',
39+
40+ // the type of data we expect back
41+ dataType : 'json',
42+
43+ // code to run if the request succeeds;
44+ // the response is passed to the function
45+ success : function(json) {
46+ $('<h1/>').text(json.title).appendTo('body');
47+ $('<div class="content"/>')
48+ .html(json.html).appendTo('body');
49+ },
50+
51+ // code to run if the request fails;
52+ // the raw request and status codes are
53+ // passed to the function
54+ error : function(xhr, status) {
55+ alert('Sorry, there was a problem!');
56+ },
57+
58+ // code to run regardless of success or failure
59+ complete : function(xhr, status) {
60+ alert('The request is complete!');
61+ }
62+ });
63+ </javascript >
6864
6965<div class =" note " markdown =" 1 " >
7066### Note
@@ -220,26 +216,24 @@ The type of data you expect back from the server. Optional.
220216This option is only applicable for methods that don't already specify the data
221217type in their name. </div >
222218
223- <div class =" example " markdown =" 1 " >
224- Using jQuery's Ajax convenience methods
225-
226- // get plain text or html
227- $.get('/users.php', { userId : 1234 }, function(resp) {
228- console.log(resp);
229- });
230-
231- // add a script to the page, then run a function defined in it
232- $.getScript('/static/js/myScript.js', function() {
233- functionFromMyScript();
234- });
235-
236- // get JSON-formatted data from the server
237- $.getJSON('/details.php', function(resp) {
238- $.each(resp, function(k, v) {
239- console.log(k + ' : ' + v);
240- });
241- });
242- </div >
219+ <javascript caption =" Using jQuery's Ajax convenience methods " >
220+ // get plain text or html
221+ $.get('/users.php', { userId : 1234 }, function(resp) {
222+ console.log(resp);
223+ });
224+
225+ // add a script to the page, then run a function defined in it
226+ $.getScript('/static/js/myScript.js', function() {
227+ functionFromMyScript();
228+ });
229+
230+ // get JSON-formatted data from the server
231+ $.getJSON('/details.php', function(resp) {
232+ $.each(resp, function(k, v) {
233+ console.log(k + ' : ' + v);
234+ });
235+ });
236+ </javascript >
243237
244238### ` $.fn.load `
245239
@@ -249,16 +243,12 @@ uses the returned HTML to populate the selected element(s). In addition to
249243providing a URL to the method, you can optionally provide a selector; jQuery
250244will fetch only the matching content from the returned HTML.
251245
252- <div class =" example " markdown =" 1 " >
253- Using ` $.fn.load ` to populate an element
246+ <javascript caption =" Using `$.fn.load` to populate an element " >
247+ $('#newContent').load('/foo.html');
248+ </javascript >
254249
255- $('#newContent').load('/foo.html');
256- </div >
257-
258- <div class =" example " markdown =" 1 " >
259- Using ` $.fn.load ` to populate an element based on a selector
260-
261- $('#newContent').load('/foo.html #myDiv h1:first', function(html) {
262- alert('Content updated!');
263- });
264- </div >
250+ <javascript caption =" Using `$.fn.load` to populate an element based on a selector " >
251+ $('#newContent').load('/foo.html #myDiv h1: first ', function(html) {
252+ alert('Content updated!');
253+ });
254+ </javascript >
0 commit comments