XMLHttpRequest (XHR) in JavaScript
Overview
XMLHttpRequest (XHR) is a built-in browser object that allows you to make HTTP requests in JavaScript. It is commonly used to interact with APIs and fetch data from a server without reloading the page.
Code Snippet Breakdown
const requestUrl = 'https://api.github.com/users/priyanshujoshi99';
const xhr = new XMLHttpRequest();
xhr.open('GET', requestUrl);
xhr.onreadystatechange = function () {
console.log(xhr.readyState);
if (xhr.readyState === 4) {
const data = JSON.parse(this.responseText);
console.log(data.name);
}
};
xhr.send();
Key Components
-
requestUrl
:- The URL to which the request is sent. In this case, it's the GitHub API endpoint for a specific user.
-
XMLHttpRequest
Object:const xhr = new XMLHttpRequest();
- Creates a new instance of the
XMLHttpRequest
object, which is used to interact with servers.
-
xhr.open(method, url)
:- Initializes the request. Here,
GET
is the HTTP method, andrequestUrl
is the endpoint. - Syntax:
xhr.open(method, url[, async[, user[, password]]])
- Initializes the request. Here,
-
xhr.onreadystatechange
:- An event handler that is called whenever the
readyState
attribute changes. readyState
indicates the state of the request:0
: UNSENT (Client has been created, butopen()
not called yet)1
: OPENED (open()
has been called)2
: HEADERS_RECEIVED (send()
has been called, headers and status are available)3
: LOADING (Downloading;responseText
holds partial data)4
: DONE (Operation is complete)
- An event handler that is called whenever the
-
xhr.readyState === 4
:- Checks if the request is complete. If true, the response is ready to be processed.
-
JSON.parse(this.responseText)
:- Parses the JSON response from the server into a JavaScript object.
this.responseText
contains the response as a string.
-
xhr.send()
:- Sends the request to the server. For
GET
requests, the body isnull
.
- Sends the request to the server. For
Additional Points
-
Asynchronous vs Synchronous:
- By default, XHR requests are asynchronous. To make a synchronous request, pass
false
as the third argument inxhr.open()
. - Example:
xhr.open('GET', requestUrl, false);
- By default, XHR requests are asynchronous. To make a synchronous request, pass
-
Error Handling:
- Always handle errors by checking the
status
property of the XHR object. - Example:
if (xhr.status === 200) {
console.log('Request successful');
} else {
console.error('Error:', xhr.statusText);
}
- Always handle errors by checking the
-
Alternatives to XHR:
- Modern JavaScript provides the
fetch
API, which is more flexible and easier to use than XHR. - Example:
fetch(requestUrl)
.then((response) => response.json())
.then((data) => console.log(data.name))
.catch((error) => console.error('Error:', error));
- Modern JavaScript provides the
-
CORS (Cross-Origin Resource Sharing):
- XHR requests are subject to CORS restrictions. Ensure the server allows requests from your domain.
-
Performance Considerations:
- XHR can block the main thread if used synchronously. Always prefer asynchronous requests for better performance.
Interesting Facts
- XHR was introduced by Microsoft in Internet Explorer 5 as an ActiveX object. It was later standardized and adopted by other browsers.
- Despite its name, XHR can be used to fetch data in formats other than XML, such as JSON, HTML, or plain text.