{"id":9849,"date":"2021-04-23T10:05:18","date_gmt":"2021-04-23T01:05:18","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9849"},"modified":"2021-04-23T13:07:10","modified_gmt":"2021-04-23T04:07:10","slug":"how-ajax-works-using-the-jquery-library","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9849","title":{"rendered":"How AJAX Works Using the jQuery Library"},"content":{"rendered":"\n<p>The jQuery ajax() method provides core functionality of AJAX in jQuery. It sends asynchronous HTTP requests to the server. <\/p>\n\n\n\n<p>The standard way of making an AJAX call using jQuery is rather simple \u2013 by using the <strong>$.ajax()<\/strong> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script>\n$.ajax(\n  '\/jquery\/getdata',\n  {\n      success: function(data) {\n        alert('AJAX call was successful!');\n        alert('Data from the server' + data);\n      },\n      error: function() {\n        alert('There was some error performing the AJAX call!');\n      }\n   }\n);\n&lt;\/script><\/code><\/pre>\n\n\n\n<p>As you already known, the <strong>$ <\/strong>sign is used to refer to a jQuery object.<\/p>\n\n\n\n<p>In the above example, the first parameter is&nbsp; string URL to which you want to submit or retrieve the data.<\/p>\n\n\n\n<p>The second parameter is the JSON format. By default, ajax() method performs http GET request if option parameter does not include method option. <\/p>\n\n\n\n<p>In most cases, we will need to specify the success and error callbacks. The success callback will be called after the successful completion of the Ajax call. On the other hand, the failure callback will be called if something goes wrong and there was an issue performing the AJAX call.<\/p>\n\n\n\n<h4><strong>Using Promises for AJAX with jQuery&nbsp;&nbsp;&nbsp;&nbsp; <\/strong>&nbsp;&nbsp;&nbsp;&nbsp; <\/h4>\n\n\n\n<p>$.ajax method supports JavaScript Promises as well.&nbsp; <\/p>\n\n\n\n<p><strong>.done()<\/strong> as replacement for <strong>.success()<\/strong><\/p>\n\n\n\n<p><strong>.fail()<\/strong> as replacement for <strong>.error()<\/strong><\/p>\n\n\n\n<p><strong>.always()<\/strong> as replacement for <strong>.complete()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.ajax({\n       data: someData,\n       dataType: 'json',\n       url: '\/path\/to\/script'\n}).done(function(data) {\n      \/\/ If successful\n      console.log(data);\n}).fail(function(jqXHR, textStatus, errorThrown) {\n      \/\/ If fail\n      console.log(textStatus + ': ' + errorThrown);\n});<\/code><\/pre>\n\n\n\n<h4><strong>Chaining Works<\/strong><\/h4>\n\n\n\n<p>Assign the <strong>$.ajax()<\/strong> method to a variable, for which we can chain promise callbacks to. In this example, the $.ajax() method returns a promise object natively, which we can use chaining too:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ajaxCall = $.ajax({\n   context: $(element),\n   data: someData,\n   dataType: 'json',\n   url: '\/path\/to\/script'\n});\n\najaxCall.done(function(data) {\n    console.log(data);\n});<\/code><\/pre>\n\n\n\n<h4><strong>Multiple AJAX calls<\/strong><\/h4>\n\n\n\n<p>We can compound several AJAX calls with their status <strong>$.when()<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a1 = $.ajax({...}),\na2 = $.ajax({...});\n\n$.when(a1, a2).done(function(r1, r2) {\n  console.log(r1[0]);\n  console.log(r2[0]);\n});<\/code><\/pre>\n\n\n\n<h4 id=\"4a9c\">Dependence chain of AJAX requests<\/h4>\n\n\n\n<p>We can also chain multiple AJAX request. For example, when the second AJAX call relies on returned data on the first call.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var a1\u200a= $.ajax({\n             url: '\/path\/to\/file',\n             dataType: 'json'\n         }),\n\n    a2 = a1.then(function(data) {\n    \/\/ .then() returns a new promise\n           return $.ajax({\n              url: '\/path\/to\/another\/file',\n              dataType: 'json',\n              data: data.sessionID\n             });\n         });\n\na2.done(function(data) {\n    console.log(data);\n});<\/code><\/pre>\n\n\n\n<p>By Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9849\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9849\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"How AJAX Works Using the jQuery Library\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9849\" data-text=\"How AJAX Works Using the jQuery Library\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9849\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9849\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>The jQuery ajax() method provides core functionality of AJAX in jQuery. It sends asynchronous HTTP requests to [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9849"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9849"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9849\/revisions"}],"predecessor-version":[{"id":9857,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9849\/revisions\/9857"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}