Introduction
The Web offers a variety of APIs to build your app or website. You could access using JavaScript code. These days the browser offers many APIs to developers. In this article, we will explore the XMLHttpRequest API.
Before moving further let us look at the previous articles of the JavaScript series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
- Voice of a Developer: JavaScript OOP - Part Eight
- Voice of a Developer: JavaScript Useful Reserved Keywords - Part Nine
- Voice of a Developer: JavaScript Functions - Part Ten
- Voice of a Developer: JavaScript Functions Invocations - Part Eleven
- Voice of a Developer: JavaScript Anonymous Functions - Part Twelve
- Voice of a Developer: JavaScript Pure And Impure Function - Part Thirteen
- Voice of a Developer: JavaScript Closures - Part Fourteen
- Voice of a Developer: JavaScript Currying - Part Fifteen
- Voice of a Developer: JavaScript Chaining - Part Sixteen
- Voice of a Developer: JavaScript Array Methods - Part Seventeen
- Voice of a Developer: JavaScript ECMAScript 6 - Part Eighteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v1 - Part Nineteen
- Voice of a Developer: JavaScript ECMAScript 6 Features v2 - Part Twenty
- Voice of a Developer: JavaScript Web Workers - Part Twenty One
- Voice of a Developer: JavaScript JSLint v1 - Part Twenty-Two
- Voice of a Developer: JavaScript JSLint v2 - Part Twenty Three
XMLHttpRequest (XHR)
* @description
* The `$http` service is a core Angular service that facilitates communication with the remote
* HTTP servers via the browser's [XMLHttpRequest]
* object or via [JSONP]
Example
In 1st example we will create XHR object and download jQuery by sending GET request,
- varmyRequest = new XMLHttpRequest(); //myRequest is an object created by XHR API
- console.log(typeof(myRequest)); // object
- myRequest.open('GET', 'https://code.jquery.com/jquery-2.2.3.min.js');
- myRequest.send();
- myRequest.onreadystatechange = function()
- {
- if (myRequest.readyState == 4 && myRequest.status == 200)
- {
- console.log(myRequest.responseText);
- }

- method: GET, POST
- url: Location of API
- async: True means asynchronous or false means synchronous
- user: If API needs authentication
- password: If API needs password
After open method is used we call send() to trigger the call at server but what if we change our code as below:
- varmyRequest = new XMLHttpRequest(); //myRequest is an object created by XHR API
- console.log(typeof(myRequest)); // object
- myRequest.open('GET','https://code.jquery.com/jquery-2.2.3.min.js');
- myRequest.send();
- console.log(myRequest.responseText); // may not get data in responseTextand there will be no error in console.
What is the benefit of onreadystatechange after we called the send method?
0: Request not initialized
1: Server connection established
2: Request received
3: Processing request
4: Request finished and response is ready
1: Server connection established
2: Request received
3: Processing request
4: Request finished and response is ready
To try this modify program like below
- varmyRequest = new XMLHttpRequest();
- myRequest.onreadystatechange = function()
- {
- switch (this.readyState)
- {
- case 0:
- console.log('request not initialized ');
- break;
- case 1:
- console.log('server connection established ');
- break;
- case 2:
- console.log('request received ');
- break;
- case 3:
- console.log('processing request ');
- break;
- case 4:
- console.log('request finished and response is ready ');
- console.log(myRequest.responseText);
- break;
- }
- }
- myRequest.open('GET', 'https://code.jquery.com/jquery-2.2.3.min.js');
- myRequest.send();

- abort: To terminate XHR request if its sent
- progress: You can monitor progress while the result is downloading
- error: In case of any error generated
Other Properties
- responseType: It could vary depending upon response sent by the server, ex- json, document, text, blob.
- responseURL: This will be the final URL after redirects.
- timeout: Number of milliseconds a request can take before terminating.
We understood one HTTP verb GET, the other common HTTP verb is POST. Posts differentiate, as you are required to pass requestHeader depending upon request ex- you are submitting a form: myRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
I am always excited to do things from the command line.
curlhttps://code.jquery.com/jquery-2.2.3.min.js
myRequest.send("fname=sumit&lname=jolly");
Command-line utility
Is there a way we can leverage command-line utility to test?
Yes, use curl command. You can download curl utility and run it:

Rajib Das GuptaPosted Jun 26, 2016, 2:45 AM
Great one
Neeraj KumarPosted May 17, 2016, 2:54 PM
Good
Kuppurasu NagarajPosted May 17, 2016, 10:55 AM
Nice sharing..
Debasis SahaPosted May 17, 2016, 9:49 AM
Nice one..
Guest UserPosted May 17, 2016, 8:36 AM
a. CORS b. JSONP c. Disable browser cross domain flag (chromium-browser --disable-web-security)
Guest UserPosted May 17, 2016, 8:35 AM
Dear Maruthi Palllamalli, thanks for reading & asking question! I think XMLHttpRequest is easy to fetch data, you may differ with it. You raised a valid point Cross Domains. There are various ways to solve this Cross Domain issue....
Maruthi PalllamalliPosted May 17, 2016, 8:05 AM
How did you say 'XMLHttpRequest ' is an easy way to get data from url. Can we get data from cross domains like from ('http://localhost:8080/Service.svc/getdata') webservices or wcf services ?
Guest UserPosted May 17, 2016, 6:45 AM
thnx Jitenfra
Guest UserPosted May 17, 2016, 6:44 AM
Thanks Bhuvanesh
Bhuvanesh MohankumarPosted May 17, 2016, 5:46 AM
Great Series
Jitendra KumarPosted May 17, 2016, 2:15 AM
Good One...Thanks for sharing..