{"componentChunkName":"component---src-templates-tag-js","path":"/tags/ajax/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":1,"edges":[{"node":{"fields":{"slug":"/engineering/ajax-and-xhr-using-plain-javascript/"},"html":"<h2 id=\"what-are-ajax-and-xhr\" style=\"position:relative;\"><a href=\"#what-are-ajax-and-xhr\" aria-label=\"what are ajax and xhr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What are Ajax and XHR?</h2>\n<p><strong>Ajax</strong> stands for Asynchronous Javascript and  XML. Ajax is a programming technique that allows us to create dynamic, complex, and asynchronous web applications. Ajax allows us to send and receive data from the webserver asynchronously without interfering with the current state or behavior of the web page or application.</p>\n<p><strong>XHR</strong> is the XMLHttpRequest Object which interacts with the server. Ajax technique in the nutshell leverages the XHR request to send and receive data from the webserver. This object is provided by the browser’s javascript environment. It transfers the data between the web browser and server.</p>\n<p><img src=\"/1513d4dbffc972d8a7d67063cf7d9886/ajax.webp\" alt=\"Ajax Call\"></p>\n<h4 id=\"key-technologies-for-incorporating-ajax--\" style=\"position:relative;\"><a href=\"#key-technologies-for-incorporating-ajax--\" aria-label=\"key technologies for incorporating ajax   permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Key technologies for incorporating AJAX -</h4>\n<ul>\n<li>HTML DOM(document object model)</li>\n<li>JSON/XML</li>\n<li>XMLHttpRequest</li>\n<li>Javascript</li>\n</ul>\n<h3 id=\"why-ajax---benefits-of-ajax\" style=\"position:relative;\"><a href=\"#why-ajax---benefits-of-ajax\" aria-label=\"why ajax   benefits of ajax permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why AJAX-  benefits of AJAX</h3>\n<p>Dynamic content modification of web page: Using Ajax reloading of a web page is not required. The content of a web page can be modified dynamically by calling the XHR request in the background and changing the content using DOM Modification.</p>\n<h3 id=\"sending-an-xhr-request\" style=\"position:relative;\"><a href=\"#sending-an-xhr-request\" aria-label=\"sending an xhr request permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sending an XHR request</h3>\n<p>To send and receive data from the server and implement the Ajax simple steps are explained below:</p>\n<ul>\n<li>Create a XMLHttpRequest object. </li>\n<li>Send the request to retrieve data from the server.</li>\n<li>Receive the response and display information to the end-user.</li>\n</ul>\n<h4 id=\"create-a-xmlhttprequest-object-\" style=\"position:relative;\"><a href=\"#create-a-xmlhttprequest-object-\" aria-label=\"create a xmlhttprequest object  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a XMLHttpRequest object :</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">XMLHttpRequest</span><span class=\"mtk1\">();</span></span></code></pre>\n<h4 id=\"send-the-request-\" style=\"position:relative;\"><a href=\"#send-the-request-\" aria-label=\"send the request  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Send the request :</h4>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">open</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&#39;GET&#39;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&#39;example.com/get&#39;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">send</span><span class=\"mtk1\">();</span></span></code></pre>\n<table>\n<thead>\n<tr>\n<th></th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>open(method, url[, async[, user[, password]]])</td>\n<td>It initializes the request.</td>\n</tr>\n<tr>\n<td><code>method</code></td>\n<td>request type such as GET,POST etc</td>\n</tr>\n<tr>\n<td><code>url</code></td>\n<td>Request URL</td>\n</tr>\n<tr>\n<td><code>Async</code></td>\n<td>true or false</td>\n</tr>\n<tr>\n<td><code>user</code></td>\n<td>Username for basic authentication</td>\n</tr>\n<tr>\n<td><code>password</code></td>\n<td>Password for basic authentication</td>\n</tr>\n<tr>\n<td>send(body)</td>\n<td>It sends the request to the server body : it is optional to send body of data with request.</td>\n</tr>\n</tbody>\n</table>\n<p>In case of sending POST request :</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">open</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;POST&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&#39;example.com/post&#39;</span><span class=\"mtk1\">, </span><span class=\"mtk4\">true</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">setRequestHeader</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;Content-type&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;application/x-www-form-urlencoded&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">send</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;username=john&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<table>\n<thead>\n<tr>\n<th></th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>setRequestHeader(header,value)</code></td>\n<td>It sets the header for the HTTP request.</td>\n</tr>\n<tr>\n<td><code>header</code></td>\n<td>name of header parameter</td>\n</tr>\n<tr>\n<td><code>Value</code></td>\n<td>value of the parameter</td>\n</tr>\n</tbody>\n</table>\n<h4 id=\"receiving-the-response-\" style=\"position:relative;\"><a href=\"#receiving-the-response-\" aria-label=\"receiving the response  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Receiving the response :</h4>\n<p>On completion of the request, the server sends the response to the request.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk11\">onreadystatechange</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\"> () {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk4\">this</span><span class=\"mtk1\">.</span><span class=\"mtk12\">readyState</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">4</span><span class=\"mtk1\"> && </span><span class=\"mtk4\">this</span><span class=\"mtk1\">.</span><span class=\"mtk12\">status</span><span class=\"mtk1\"> == </span><span class=\"mtk7\">200</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">document</span><span class=\"mtk1\">.</span><span class=\"mtk11\">getElementById</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;response&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk12\">innerHTML</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">xhrobj</span><span class=\"mtk1\">.</span><span class=\"mtk12\">responseText</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">  }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<table>\n<thead>\n<tr>\n<th></th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>onreadystatechange = callback()</code></td>\n<td>It is a EventHandler called when the readyState attribute changes.</td>\n</tr>\n<tr>\n<td><code>readyState</code> attribute</td>\n<td>It is an attribute that returns the current state of XMLHttpRequest object</td>\n</tr>\n<tr>\n<td><code>status</code> attribute</td>\n<td>It is an attribute that returns the status code to the HTTP XHR request.</td>\n</tr>\n<tr>\n<td><code>responseText</code> attribute</td>\n<td>It is an attribute that returns the DOMstring response as the text.</td>\n</tr>\n</tbody>\n</table>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>Implementing the Ajax technique by using the XHR in javascript instead of going with using other javascript library functions like jQuery.ajax has advantages as well. For example, it gives you the freedom to embed your application or script with other applications or platform even if the particular library is not used by other applications. Thus it also helps in code reusability.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"date":"September 22, 2020","updated_date":null,"title":"Ajax and XHR using plain JS","tags":["XHR","JavaScript","AJAX"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/c19cc11ae05c7a1224ef47b974bbf68f/58556/index.webp","srcSet":"/static/c19cc11ae05c7a1224ef47b974bbf68f/61e93/index.webp 200w,\n/static/c19cc11ae05c7a1224ef47b974bbf68f/1f5c5/index.webp 400w,\n/static/c19cc11ae05c7a1224ef47b974bbf68f/58556/index.webp 800w,\n/static/c19cc11ae05c7a1224ef47b974bbf68f/99238/index.webp 1200w,\n/static/c19cc11ae05c7a1224ef47b974bbf68f/135cd/index.webp 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Apeksha Gupta","github":"ApekshaAgarwal","avatar":null}}}}]}},"pageContext":{"tag":"AJAX"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}