{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/ajax","result":{"data":{"markdownRemark":{"html":"<h3>What is AJAX?</h3>\n<p>AJAX stands for Asynchronous JavaScript and XML. It is a way for front-end code to communicate with the backend by making <em>asynchronous</em> API calls.\nIt allows you to update small parts of a web page, rather than refreshing the whole page.\nFor example: Posting a tweet on Twitter. The tweet will appear after you press “submit”, but the page itself will not be reloaded.</p>\n<h3>Common Mistakes / Misconceptions</h3>\n<ul>\n<li>Most web applications that use AJAX don't use XML anymore, despite the name \"AJAX\"; instead, they transport data using other formats, most commonly as JSON.</li>\n<li>AJAX is technically not an API and it is not a library in and of itself. It is a set of <em>techniques</em> that uses the XMLHttpRequest (XHR) API. There are many JavaScript libraries, including jQuery, that contain wrappers around the XHR API, and these are colloquially called AJAX libraries.</li>\n<li>Because AJAX is <em>asynchronous</em>, you must pass in a callback function to handle the received data.</li>\n</ul>\n<h3>Threads of Execution</h3>\n<p>A <em>thread of execution</em> in <a href=\"https://simple.wikipedia.org/wiki/Computer_science\" title=\"Computer science\">computer science</a> is a way for a <a href=\"https://simple.wikipedia.org/wiki/Computer_program\" title=\"Computer program\">program</a> to divide (termed <em>\"split\"</em>) itself into two or more simultaneously running [tasks].</p>\n<p>Javascript runs in a single thread, which simply means it runs one command at a time. Events are <a href=\"https://en.wikipedia.org/wiki/Queue_(abstract_data_type)\">queued</a> and executed in the order they are written.</p>\n<h3>Examples of an AJAX call</h3>\n<p>You can basically use it to get data from different <strong>APIs</strong>.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\">$<span class=\"token punctuation\">.</span><span class=\"token function\">ajax</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"some-url\"</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function-variable function\">success</span><span class=\"token punctuation\">:</span> <span class=\"token parameter\">data</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">/* do something with the data */</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token function-variable function\">error</span><span class=\"token punctuation\">:</span> <span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">/* do something when an error happens */</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2>Make fetch happen</h2>\n<p>The Fetch API provides an interface for fetching resource. It is similar to the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest\" title=\"Use XMLHttpRequest (XHR) objects to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.\"><code class=\"language-text\">XMLHttpRequest</code></a>, but the new API provides a more powerful and flexible feature set.</p>\n<p>Getting data with Fetch is easier. You just need to provide Fetch with the resource you're trying to fetch.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">async</span> <span class=\"token keyword\">function</span> <span class=\"token function\">getData</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">//await the response of the fetch call</span>\n  <span class=\"token keyword\">let</span> response <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"https://api.github.com/users\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token comment\">//proceed once the first promise is resolved.</span>\n  <span class=\"token keyword\">let</span> data <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> response<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token comment\">//proceed only when the second promise is resolved</span>\n  <span class=\"token keyword\">return</span> data\n<span class=\"token punctuation\">}</span>\n<span class=\"token comment\">//call getData function</span>\n<span class=\"token function\">getData</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">data</span> <span class=\"token operator\">=></span> console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token comment\">//To do something after the resource is fetched, you write it in a  `.then`  call:</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Fetch returns a <a href=\"https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261\">Promise</a>, which is a way to handle asynchronous operations without the need for a callback.</p>\n<p>Support for Fetch is great. All major browsers (with the exception of Opera Mini and old IE) support it natively, which means you can safely use it. If you need support anywhere it isn't natively supported, you can always depend on <a href=\"https://github.com/github/fetch\">this handy polyfill</a>.</p>","excerpt":"What is AJAX? AJAX stands for Asynchronous JavaScript and XML. It is a way for front-end code to communicate with the backend by making asynchronous API calls…","frontmatter":{"date":"25 February, 2020","path":"/blog/ajax","title":"Let's fetch AJAX "},"fields":{"readingTime":{"text":"3 min read"}}}},"pageContext":{}}}