Introduction
XMLHTTPRequest is an object which is used to perform the Asynchronous HTTP call using JavaScript. Usually, we call it an AJAX call. It is a browser object which is supported by all modern browsers and basically, it can handle any type of data which is used to communicate between the client and server using HTTP.
Asynchronous HTTP call
AsyncDemo.HTML
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- </head>
- <body>
- <button id="btnAsync" type="button">Click Me</button>
- </body>
- <script>
- (function () {
- document.getElementById("btnAsync").addEventListener('click', makeRequest);//attaching click event for button
- function makeRequest() {
- var httpRequest = new XMLHttpRequest();// Initiatlization of XMLHttpRequest
- if (!httpRequest) {
- alert(' Cannot create an XMLHTTP instance');
- return false;
- }
- httpRequest.onreadystatechange = function () { // ready state event, will be executed once the server send back the data
- if (httpRequest.readyState === XMLHttpRequest.DONE) {
- if (httpRequest.status === 200) {
- alert(httpRequest.responseText);
- } else {
- alert('There was a problem with the request.');
- }
- }
- };
- httpRequest.open('GET', 'http://localhost:11207/api/Customer/CustomerDetails'); // service call
- httpRequest.send();
- }
- })();
- </script>
- </html>



Join the conversation! Your thoughts help the community grow.