{"id":16897,"date":"2017-04-20T16:15:50","date_gmt":"2017-04-20T13:15:50","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=16897"},"modified":"2017-12-19T13:02:41","modified_gmt":"2017-12-19T11:02:41","slug":"html5-xmlhttprequest-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/","title":{"rendered":"HTML5 XMLHttpRequest Example"},"content":{"rendered":"<p>As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known as AJAX.<\/p>\n<p>The XMLHttpRequest object is far from being new: it&#8217;s more than 10 years old. But, with HTML5, a &#8220;level 2&#8221; of this object was introduced, with some improvements.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;tgm4cmEWcKUikZNn&#8217;]<br \/>\n&nbsp;<br \/>\nFor this example, the following browsers have been used for testing:<\/p>\n<ul>\n<li>Chromium\u00a056.0.2924.76.<\/li>\n<li>Firefox 52.0.1.<\/li>\n<li>Opera 44.0.<\/li>\n<\/ul>\n<p>And, apart from the browser, we will install a web server, running in Linux Mint 18.1.\u00a0<strong>Not to use any server-side language<\/strong>, but\u00a0for a reason we will see later.<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip web server installation\u00a0and jump directly to the <a href=\"#section_2\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<h2>1. Installing the web server<\/h2>\n<p>As said before, we are not using any server-side language, so we just need a web server, without any interpreter for any language.<\/p>\n<p>For example, we can install Nginx:<\/p>\n<pre class=\"brush:bash\">sudo apt-get update\r\nsudo apt-get install nginx\r\n<\/pre>\n<p>After the installation the web server service should automatically start, and, if we access <code>localhost<\/code> or <code>127.0.0.1<\/code>, we should see the Nginx welcome page.<\/p>\n<h2 id=\"section_2\">2. Transferring BLOBs<\/h2>\n<p>One of the improvements was the option of allowing to transfer binary data. The simplest case can be to GET or POST some image, for example.<\/p>\n<p>Let&#8217;s imagine that we want to have a page, with a button that, when clicked, creates a request for getting an image and show it. Obviously, images are binary data, which is something to take into account for making these requests.<\/p>\n<p>We have chosen getting an image instead of posting one, not to have to install a web server. That means that we have to GET an image from some server. So, let&#8217;s GET the WebCodeGeeks logo, for example.<\/p>\n<p>The HTML would just consist on including the script, a button and an image element, apart from additional styles:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>1_blob.html<\/em><\/span><\/p>\n<pre class=\"brush:html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;1 - BLOBs&lt;\/title&gt;\r\n    &lt;script src=\"1_blob.js\"&gt;&lt;\/script&gt;\r\n    &lt;style&gt;\r\n        #wrapper {\r\n            width: 60%;\r\n            margin: 0 auto;\r\n            text-align: center;\r\n        }\r\n        #img {\r\n            display: block;\r\n            margin: auto;\r\n        }\r\n    &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"wrapper\"&gt;\r\n        &lt;h1&gt;1 - BLOBs&lt;\/h1&gt;\r\n        &lt;button id=\"button\"&gt;Click me!&lt;\/button&gt;\r\n        &lt;img id=\"img\"&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>If we open it with a browser, we will just see a blank page. with a header and a button.<\/p>\n<figure style=\"width: 583px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/04\/blobs_empty.jpg\" alt=\"\" width=\"583\" height=\"349\" \/><figcaption class=\"wp-caption-text\">1. The HTML page, before the making the request.<\/figcaption><\/figure>\n<p>The interesting part is the script, let&#8217;s see it:<\/p>\n<p><em><span style=\"text-decoration: underline;\">1_blob.js<\/span><\/em><\/p>\n<pre class=\"brush:javascript\">document.addEventListener('DOMContentLoaded', function(domEvent) {\r\n    document.getElementById('button').onclick = function(clickEvent) {\r\n        var xhr = new XMLHttpRequest(),\r\n            resource = 'http:\/\/localhost\/WebCodeGeeks-logo.png';\r\n\r\n        xhr.onreadystatechange = function() {\r\n            var finishedAndSuccess = this.readyState == 4 &amp;&amp; this.status == 200,\r\n                finishedAndError = this.readyState == 4 &amp;&amp; this.status != 200;\r\n\r\n            if (finishedAndSuccess) {\r\n                var img = document.getElementById('img');\r\n                var url = window.URL || window.webkitURL;\r\n                img.src = url.createObjectURL(this.response);\r\n            } else if (finishedAndError) {\r\n                alert('An error occured in the request, check the log.');\r\n            }\r\n        }\r\n\r\n        xhr.open('GET', resource);\r\n        xhr.responseType = 'blob';\r\n\r\n        xhr.send();\r\n    } \r\n});\r\n<\/pre>\n<p>The first listener is to load the second listener (the one setting the onclick event) when the whole document is loaded, actually nothing to see with the request.<\/p>\n<p>The first step is to instantiate the <code>XMLHttpRequest<\/code> class, often shortened to <code>xhr<\/code>. Note that we don&#8217;t have to specify that it&#8217;s its second version or similar.<\/p>\n<p>Then, we set the handler to execute every time the request state changes. The request suffers changes on its state several times from the very beginning, to its end; it&#8217;s not just beginning\/end. This is way we have to check that the state of the request is <code>4<\/code> (finished), apart from checking that it was successful (HTTP <code>200<\/code> code).<\/p>\n<p>Checking if the request was successful or not doesn&#8217;t have any\u00a0secret: if it was successful, we just add the image to its corresponding tag; and, if not, we show the error.<\/p>\n<p>After defining what to do when the request is ended and successful, we have to <code>open<\/code> it, specifying the method and the resource; set that the response type is <code>blob<\/code>, and <code>send<\/code> it.<\/p>\n<p>If we now try to click the button&#8230; Nothing happened. In theory,\u00a0our script is correct. But this is a special case for which we have to do something different, but it&#8217;s not related to the transfer of the BLOB.<\/p>\n<p>If we check the console of the browser, we should see an error message similar to the following one:<\/p>\n<blockquote><p>XMLHttpRequest cannot load https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/12\/WebCodeGeeks-logo.png. No &#8216;Access-Control-Allow-Origin&#8217; header is present on the requested resource. Origin &#8216;null&#8217; is therefore not allowed access.<\/p><\/blockquote>\n<p>By default, for security reasons, browsers (should) block AJAX\u00a0requests\u00a0to different domains, which are known as CORS (Cross-Origin Resource Sharing). <strong>If a web server doesn&#8217;t allow CORS, there&#8217;s no chance for the client: it won&#8217;t be able to get the resource<\/strong>.<\/p>\n<p><strong>Note<\/strong>: actually, transferring BLOBs is not the only change in what to data format regards. We can also set, besides <code>text<\/code>, <code>arraybuffer<\/code> or <code>document<\/code>.<\/p>\n<h3>2.1. Making the request to our own server<\/h3>\n<p><strong>Note<\/strong>: these steps are not necessary. The code above is correct and it will work for contents that are allowed to be shared from other domains. What we make here is just to test it with a server that we make sure it allows CORS (our own local server).<\/p>\n<p>The first thing is to allow CORS in Nginx:<\/p>\n<pre class=\"brush:bash\">cd \/var\/www\/html\/<\/pre>\n<p>So, for testing this, we need a server that allows cross-origin resource sharing. That&#8217;s for what we installed our own server.<\/p>\n<p>Before, we have configure Nginx to send the <code>Access-Control-Allow-Origin<\/code> HTTP header to the client:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>\/etc\/nginx\/sites-enabled\/default<\/em><\/span><\/p>\n<pre class=\"brush:bash\"># ...\r\n\r\nserver {\r\n    # ...\r\n    add_header 'Access-Control-Allow-Origin' '*';\r\n    # ...\r\n}\r\n<\/pre>\n<p>Now, we have to place our HTML and JavaScript files in the web directory, <code>\/var\/www\/html\/<\/code>, to access the HTML file via web browser, instead of locally. In my case, <code>http:\/\/localhost\/html5_xmlhttprequest_example\/1_blobs\/1_blob.html<\/code>.<\/p>\n<p>Now, we have to download the image to our server:<\/p>\n<pre class=\"brush:bash\">cd \/var\/www\/html\/\r\nsudo wget https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/12\/WebCodeGeeks-logo.png\r\nsudo chown www-data:www-data WebCodeGeeks-logo.png<\/pre>\n<p>Of course, we have to update our JavaScript file to GET the image locally:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>1_blob.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript\">\/\/ ...\r\nresource = 'http:\/\/localhost\/WebCodeGeeks-logo.png';\r\n\/\/ ...\r\n<\/pre>\n<p>If we now access the HTML file and click the button, we will see the image!<\/p>\n<figure style=\"width: 583px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/04\/blobs_image.jpg\" alt=\"\" width=\"583\" height=\"349\" \/><figcaption class=\"wp-caption-text\">2. The HTML page, after the request.<\/figcaption><\/figure>\n<h2>3. Setting a timeout<\/h2>\n<p>Setting a timeout is something we should do for most of the times\u00a0we request an external resource, regardless the technique or method, specially asynchronous ones. Because we shouldn&#8217;t wait leave the possibility of waiting forever for the request, unless it&#8217;s explicitly necessary for the scenario.<\/p>\n<p>With the level 2 of XHR, we can easily set a timeout for a request. For this, we will use the same HTML as for the example before (just changing the title of the page, the header, and the name to the script.<\/p>\n<p>The script would be almost the same:<\/p>\n<p><em><span style=\"text-decoration: underline;\">2_timeout.js<\/span><\/em><\/p>\n<pre class=\"brush:javascript\">document.addEventListener('DOMContentLoaded', function(domEvent) {\r\n    document.getElementById('button').onclick = function(clickEvent) {\r\n        var xhr = new XMLHttpRequest(),\r\n            resource = 'http:\/\/localhost\/WebCodeGeeks-logo.png';\r\n\r\n        xhr.onreadystatechange = function() {\r\n            var finishedAndSuccess = this.readyState == 4 &amp;&amp; this.status == 200,\r\n                finishedAndError = this.readyState == 4 &amp;&amp; this.status != 200;\r\n\r\n            if (finishedAndSuccess) {\r\n                var img = document.getElementById('img');\r\n                var url = window.URL || window.webkitURL;\r\n                img.src = url.createObjectURL(this.response);\r\n            } else if (finishedAndError) {\r\n                alert('An error occured in the request, check the log.');\r\n            }\r\n        }\r\n\r\n        xhr.open('GET', resource);\r\n        xhr.responseType = 'blob';\r\n        xhr.timeout = 100; \/\/ milliseconds.\r\n\r\n        xhr.send();\r\n    } \r\n});\r\n<\/pre>\n<p>As we can see, it&#8217;s just about setting a value to the <code>timeout<\/code> attribute of our <code>xhr<\/code> object, in milliseconds.<\/p>\n<p>Additionally, we can set an <code>ontimeout<\/code> event for executing a piece of code if the timeout is reached:<\/p>\n<p><em><span style=\"text-decoration: underline;\">2_timeout.js<\/span><\/em><\/p>\n<pre class=\"brush:javascript\">\/\/ ...\r\nxhr.ontimeout = function(timeoutEvent) {\r\n    alert('Timeout reached');\r\n}\r\n\r\n\/\/ ...<\/pre>\n<h2>4.\u00a0Setting key\/value as attributes of the request<\/h2>\n<p>In the previous specification, when we wanted to send data to the server, we had to define a string with the format <code>key1=value1&amp;key2=value2<\/code>, which is actually quite dirty and unreadable.<\/p>\n<p>In this second level of XMLHttpRequest, we can set this data in a much more tidier way, thanks to the <code>FormData<\/code> object.<\/p>\n<p>Before seeing how, let&#8217;s define a HTML page with some inputs for the user:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>3_sending_data.html<\/em><\/span><\/p>\n<pre class=\"brush:html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;3 - Sending data&lt;\/title&gt;\r\n    &lt;script src=\"3_sending_data.js\"&gt;&lt;\/script&gt;\r\n    &lt;style&gt;\r\n        #wrapper {\r\n            width: 60%;\r\n            margin: 0 auto;\r\n            text-align: center;\r\n        }\r\n        .row {\r\n            margin-bottom: 20px;\r\n        }\r\n    &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div id=\"wrapper\"&gt;\r\n        &lt;h1&gt;3 - Sending data&lt;\/h1&gt;\r\n\r\n        &lt;div class=\"row\"&gt;\r\n            &lt;input type=\"text\" name=\"key1\" id=\"key1\" placeholder=\"Key 1\" required&gt;\r\n            &lt;input type=\"text\" name=\"value1\" id=\"value1\" placeholder=\"Value 1\" required&gt;\r\n        &lt;\/div&gt;\r\n\r\n        &lt;div class=\"row\"&gt;\r\n            &lt;input type=\"text\" name=\"key2\" id=\"key2\" placeholder=\"Key 2\" required&gt;\r\n            &lt;input type=\"text\" name=\"value2\" id=\"value2\" placeholder=\"Value 2\" required&gt;\r\n        &lt;\/div&gt;\r\n\r\n        &lt;button id=\"button\"&gt;Send&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now, the JavaScript:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>3_sending_data.js<\/em><\/span><\/p>\n<pre class=\"brush:javascript\">document.addEventListener('DOMContentLoaded', function(domEvent) {\r\n    document.getElementById('button').onclick = function(clickEvent) {\r\n        var xhr = new XMLHttpRequest(),\r\n            requestData = new FormData();\r\n\r\n        xhr.onreadystatechange = function() {\r\n            var finishedAndSuccess = this.readyState == 4 &amp;&amp; this.status == 200,\r\n                finishedAndError = this.readyState == 4 &amp;&amp; this.status != 200;\r\n\r\n            if (finishedAndSuccess) {\r\n                alert('Success! (you can check the request with the dev tools)');\r\n            } else if (finishedAndError) {\r\n                alert('An error occured in the request, check the log.');\r\n            }\r\n        }\r\n\r\n        var key1 = document.getElementById('key1').value,\r\n            value1 = document.getElementById('value1').value,\r\n            key2 = document.getElementById('key2').value,\r\n            value2 = document.getElementById('value2').value;\r\n\r\n        xhr.open('POST', '3_sending_data.html');\r\n\r\n        requestData.append(key1, value1);\r\n        requestData.append(key2, value2);\r\n\r\n        xhr.send(requestData);\r\n    } \r\n});\r\n<\/pre>\n<p>In this case, as we don&#8217;t expect any answer from the server, we just make the checks on the status for showing if it was successful or not, nothing else.<\/p>\n<p>The work has to be done before checking the state. For this situation, we have set the HTML itself as endpoint, in <code>open<\/code>, even if it won&#8217;t process the data; but if we set an non-existing file, or an endpoint of another domain, we will get an error (404 and CORS non-allowed, respectively).<\/p>\n<p>After retrieving the values, we instantiate a <code>FormData<\/code> object, and we append them in <code>(key, value)<\/code> format. Finally, we just have to pass this object to the <code>send<\/code> method.<\/p>\n<p>As we don&#8217;t process this data in the backend, we can check that the data has been transferred correctly with the developer tools of the browser. The following is the output for Chromium.<\/p>\n<figure style=\"width: 409px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/04\/posting_data.jpg\" alt=\"\" width=\"409\" height=\"197\" \/><figcaption class=\"wp-caption-text\">3. Checking the posted data with development tools.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h2>5. Summary<\/h2>\n<p>This example has shown the improvements of the specification of XMLHttpRequest object for HTML5. This improvements include the specification of the file format, when before we could just send data in text format; setting a timeout for the request, to avoid possible infinite or very large requests, may caused by a high latency or huge files; and the improvement of setting the data, in key-value format, with the <code>FormData<\/code> object.<\/p>\n<h2>6. Download the source code<\/h2>\n<p>This was an example of HTML5 XMLHttpRequest example.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here:\u00a0<a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/04\/HTML5XMLHttpRequestExample.zip\"><strong>HTML5XMLHttpRequestExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known as AJAX. The XMLHttpRequest object is far from being new: it&#8217;s more than 10 years old. But, with HTML5, a &#8220;level 2&#8221; of this object was introduced, with some improvements. &nbsp; &hellip;<\/p>\n","protected":false},"author":160,"featured_media":914,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-16897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html5"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HTML5 XMLHttpRequest Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTML5 XMLHttpRequest Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-20T13:15:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-19T11:02:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Toni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Toni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\"},\"author\":{\"name\":\"Toni\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\"},\"headline\":\"HTML5 XMLHttpRequest Example\",\"datePublished\":\"2017-04-20T13:15:50+00:00\",\"dateModified\":\"2017-12-19T11:02:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\"},\"wordCount\":1348,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg\",\"articleSection\":[\"HTML5\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\",\"name\":\"HTML5 XMLHttpRequest Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg\",\"datePublished\":\"2017-04-20T13:15:50+00:00\",\"dateModified\":\"2017-12-19T11:02:41+00:00\",\"description\":\"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HTML5\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/html5\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"HTML5 XMLHttpRequest Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\",\"name\":\"Toni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"Toni\"},\"url\":\"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HTML5 XMLHttpRequest Example - Web Code Geeks - 2026","description":"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/","og_locale":"en_US","og_type":"article","og_title":"HTML5 XMLHttpRequest Example - Web Code Geeks - 2026","og_description":"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known","og_url":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2017-04-20T13:15:50+00:00","article_modified_time":"2017-12-19T11:02:41+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg","type":"image\/jpeg"}],"author":"Toni","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Toni","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/"},"author":{"name":"Toni","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966"},"headline":"HTML5 XMLHttpRequest Example","datePublished":"2017-04-20T13:15:50+00:00","dateModified":"2017-12-19T11:02:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/"},"wordCount":1348,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg","articleSection":["HTML5"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/","url":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/","name":"HTML5 XMLHttpRequest Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg","datePublished":"2017-04-20T13:15:50+00:00","dateModified":"2017-12-19T11:02:41+00:00","description":"As you probably already know, the XMLHttpRequest object is used for making asynchronous requests from the client to the server, with the technique known","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/html5-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/html5\/html5-xmlhttprequest-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"HTML5","item":"https:\/\/www.webcodegeeks.com\/category\/html5\/"},{"@type":"ListItem","position":3,"name":"HTML5 XMLHttpRequest Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966","name":"Toni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"Toni"},"url":"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/16897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/160"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=16897"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/16897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/914"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=16897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=16897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=16897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}