How to Make Ajax Call Without jQuery

Habdul Hazeez Feb 02, 2024
  1. How to Make Ajax Call With XMLHttpRequest in JavaScript
  2. How to Make Ajax Call With Fetch API in JavaScript
How to Make Ajax Call Without jQuery

This tutorial will teach you how to make an AJAX call in JavaScript without the need for an external library. Therefore, we’ll teach you the following.

  1. How to make an AJAX call with XMLHttpRequest
  2. How to make an AJAX call with the Fetch API

How to Make Ajax Call With XMLHttpRequest in JavaScript

The XMLHttpRequest is an object in JavaScript that allows you to make an AJAX call. It makes this possible thanks to its suite of methods.

These methods include:

  1. XMLHTTPRequest.onreadystatechange()
  2. XMLHTTPRequest.readyState
  3. XMLHTTPRequest.status()
  4. XMLHTTPRequest.open()
  5. XMLHTTPRequest.send()

If combined correctly, these methods will allow you to make an AJAX call. Also, the object names give you an idea of what they do.

For example, XMLHTTPRequest.send() will send the AJAX call when you are ready. The following is a typical algorithm for using XMLHttpRequest for an AJAX call.

  1. Create a new XMLHTTPRequest object

  2. Attach a function to XMLHTTPRequest.onreadystatechange()

    2.1 The function will check if XMLHTTPRequest.readyState returns DONE
    2.2 Check XMLHTTPRequest.status() and act accordingly

  3. Open the AJAX connection

    3.1 This is the part where you specify if the connection will be a GET or POST request.

  4. Send the AJAX call

Meanwhile, depending on what you are trying to achieve, you might have a longer algorithm. However, for our example detailed next, the algorithm serves our purpose.

The user can initiate an AJAX call by clicking on the HTML button in the example. The call will replace the content of the <main> element.

<head>
	<meta charset="utf-8">
	<title>Plain AJAX Request</title>
	<style>
		body {
			display: grid;
			justify-content: center;
			align-items: center;
			height: 100vh;
		}

		button {
			padding: 1.2em;
			background-color: #1a1a1a;
			color: #ffffff;
			border: none;
			cursor: pointer;
		}
	</style>
</head>
<body>
	<main id="button_container">
		<button type="button"  onclick="load_ajax_button()">CLICK ME</button>
	</main>
	<script>
		function load_ajax_button() {
			const xhttp = new XMLHttpRequest();
			xhttp.onload = function() {
				let button_container = document.getElementById("button_container")
				button_container.innerHTML = this.responseText;
			}
			xhttp.open("GET", "ajax-button.html", true);
			xhttp.send();
		}
	</script>
</body>

Output:

Ajax Request With XMLHttpRequest

How to Make Ajax Call With Fetch API in JavaScript

The Fetch API is a built-in feature of modern web browsers that allows you to make AJAX calls. What’s more, you can consider the Fetch API as a modern version of XMLHttpRequest.

For example, in our code below, every time you press the HTML button, we fetch a random image from the Picsum API. Meanwhile, the following is how the process works.

  1. Click the HTML button to initiate the Fetch request to the Picsum API.
  2. We grab the url property from the Picsum API response. That’s because the url contains the address of the random image.
  3. Use the address from the Picsum API as the image source.
  4. Therefore, every time you click the HTML button, you get a new random image.
<head>
	<meta charset="utf-8">
	<title>Fetch API</title>
	<style>
		main {
			text-align: center;
			margin: 0 auto;
			max-width: 800px;
		}

		img {
			max-width: 100%;
			display: inline-block;
		}

		button {
			margin-top: 20px;
		}
	</style>
</head>
<body>
	<main>
		<h2>A Simple Fetch API Example</h2>
		<div class="img-container">
			<img src="" id="random_image" width="300" height="300">
		</div>
		<button>Click Me for a Random Image</button>
	</main>

	<script>
		let button = document.querySelector('button');

		button.addEventListener('click', function () {
			fetch('https://picsum.photos/200/300')
			.then((response) => {
				return response;
			})
			.then((image_source) => {
				random_image.src = image_source['url'];
			});
		}, false);
	</script>
</body>

Output:

Ajax with the Fetch API

Habdul Hazeez avatar Habdul Hazeez avatar

Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.

LinkedIn

Related Article - JavaScript Ajax