Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
XMLHttpRequest Object
Sending Request
XMLHttpRequest Object Methods
Characteristics of XMLHttpRequest Object
Frequently Asked Question
What is the purpose of an XMLHttpRequest?
When was XMLHttpRequest introduced?
How do I send an XHR request?
What is the complete form of HTTP?
What is an HTTP status code? 
Last Updated: Mar 27, 2024

AJAX - The XMLHttpRequest Object

Roadmap to SDE career at Amazon
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM


A collection of Ajax methods leverages several client-side web technologies to build asynchronous online applications. Ajax enables web applications to transmit and receive data asynchronously from a server without changing how a page looks or functions.

 AJAX - The XMLHttpRequest Object

This blog will provide an in-depth discussion of the XMLHttpRequest object and usage examples.

To know more about Cloud Computing click here.

XMLHttpRequest Object

The XMLHttpRequest (XHR) objects are used for server communication. The Ajax XMLHttpRequest object can make many things more accessible. Using this object, we can make HTTP requests and receive responses from the server without requiring the user to submit the page to the server.

In layman's terms, we can get data from a URL without reloading the complete page. This allows a Web page to update only a portion of the page without interrupting the user's current activity. We can create a highly user-friendly web application using this object.


A built-in XMLHttpRequest object is available in all contemporary browsers like Safari, Chrome, Firefox, Edge, and Opera).


variableName = new XMLHttpRequest();


Another function receives a callback function as an argument. The program running when the response is prepared in this scenario should be in the callback function.

XHRObject.onload = function() {
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Sending Request

The XMLHttpRequest object's open() and send() methods can be used to deliver requests to servers:

Syntax:"GET", "path_or_url");


<!DOCTYPE html>
<link rel="stylesheet" href="pathToYour/bootstrap.min.css"/>
	<body class="p-3 m-0 border-0 bd-example">
		<h2>The XMLHttpRequest Object</h2>
		<div id="demo">
			<button class="btn btn-outline-primary" type="button" onclick="loadDoc()">GET</button> Status Code :
		<div id="get">
				function loadDoc() {
					const XHRobj = new XMLHttpRequest();
					XHRobj.onload = function() {
						document.getElementById("get").innerHTML = this.status;
					}"GET", "");


Before clicking:

Before Clicking

After clicking on "GET":

After Clicking

Try this code on the online javascript compiler for better understanding.

XMLHttpRequest Object Methods

The table below contains some of the essential methods of the XMLHttpRequest Object.



new XMLHttpRequest() Creates a new XMLHttpRequest object.
send() For GET requests, send the request to the server.
send(string) For POST requests, send the request to the server.
setRequestHeader() A label/value pair is added to the header that will be sent.
getResponseHeader() Returns the specific header information.
getAllResponseHeaders() Returns header information.
abort() Cancels the current request.
open(method, url, async, user, psw)

Explains the request

method: the request type GET or POST 

url: the file location

async: true (asynchronous) or false (synchronous)

user: optional user name 

psw: optional password


Characteristics of XMLHttpRequest Object

Character Description

This property holds the status of the XMLHttpRequest.

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request is finished, and the response is ready

onreadystatechange This property defines a function called when the property of readyState changes. The status of the XMLHttpRequest object is stored in the status and statusText fields.
onload Define a callback function. This function will execute when the request receives a response.
responseText This returns the response data in the form of a string.
responseXML This returns the response data in the form of XML data.

Returns the status number of a request

200: "OK"

403: "Forbidden"

404: "Not Found."

statusText Returns the status text (e.g., "OK" or "Not Found")

Frequently Asked Question

What is the purpose of an XMLHttpRequest?

Interacting with servers requires the usage of XMLHttpRequest (XHR) objects. We can get data from a URL without doing a full page refresh.

When was XMLHttpRequest introduced?

Since Gecko 0.6, published on December 6, 2000, users have had access to the XMLHttpRequest object.

How do I send an XHR request?

We use the XMLHttpRequest object's open() and send() methods to send a request to a server.

What is the complete form of HTTP?

Hypertext Transfer Protocol.

What is an HTTP status code? 

A website's server informs the browser with the HTTP status code to let them know whether or not the request may be processed.


Let's summarize everything we have learned in this blog. We got to know what XMLHttpRequest (XHR) and XMLHttpRequest objects are. After that, we discussed XMLHttpRequest object properties and methods and how to use them. 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Previous article
AJAX introduction
Next article
JS Ajax Request
Live masterclass