Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. XMLHttpRequest

XMLHttpRequest

The traditional method for making HTTP requests to a server. It allows you to fetch and send data without reloading the page, and has long served as the foundation for Ajax communication.

Syntax

var xhr = new XMLHttpRequest();
xhr.open("method", "URL");
xhr.onreadystatechange = function() {
	if (xhr.readyState === 4 && xhr.status === 200) {
		// Code to run on success
	}
};
xhr.send();

Methods

MethodDescription
open("method", "URL")Initializes the request. The first argument specifies the HTTP method, and the second specifies the URL. Passing false as the third argument makes the request synchronous, but this is not recommended.
send()Sends the request. For POST requests, pass the data to send as an argument.
setRequestHeader("name", "value")Sets a request header. Must be called after open() and before send().

Properties

PropertyDescription
readyStateReturns the current state of the request as a number. 0 = unsent, 1 = opened, 2 = headers received, 3 = loading, 4 = done.
statusReturns the HTTP status code. 200 means success, 404 means not found, and 500 means a server error.
responseTextReturns the server's response as a string.
onreadystatechangeSets an event handler that is called whenever readyState changes.

Sample Code

// Example: fetching data with a GET request
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/users");
xhr.onreadystatechange = function() {
	if (xhr.readyState === 4) { // Request complete
		if (xhr.status === 200) { // Success
			var data = JSON.parse(xhr.responseText); // Parse the JSON string into an object.
			console.log(data);
		} else {
			console.log("Error: " + xhr.status); // Log the status code.
		}
	}
};
xhr.send();
// Example: sending data with a POST request
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.example.com/users");
xhr.setRequestHeader("Content-Type", "application/json"); // Tell the server we're sending JSON.
xhr.onreadystatechange = function() {
	if (xhr.readyState === 4 && xhr.status === 200) {
		console.log("Registered: " + xhr.responseText);
	}
};
var sendData = JSON.stringify({ name: "Taro", age: 25 });
xhr.send(sendData); // Send the JSON string.

Overview

XMLHttpRequest is an object used to send HTTP requests from JavaScript to a server. It has been the core of a technique called "Ajax," which allows pages to exchange data with a server without reloading, and has been in use for many years.

The communication flow follows four steps: create an XMLHttpRequest object, configure the request with open(), set a callback with onreadystatechange, and send with send(). When readyState reaches 4, the request is complete — at that point you check status and process the response.

Today, fetch() is recommended as a simpler alternative. XMLHttpRequest is callback-based and can be verbose, but it remains essential to understand when you need compatibility with older browsers or when maintaining existing code.

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
1 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11+
6 ×
5
↑ Partial support
4 or earlier ×
Opera Opera
48+
7 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
2 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .