Pooja Mishra
2 min readJun 18, 2024

Some short descriptions of various API call methods in JavaScript, along with examples.

1. Using XMLHttpRequest

XMLHttpRequest is an older way to make HTTP requests in JavaScript. It is a bit more complex compared to newer methods.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data', true);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
console.log('Response:', xhr.responseText);
} else {
console.log('Error:', xhr.statusText);
}
};
xhr.onerror = function () {
console.log('Request failed');
};
xhr.send();

2. Using fetch

fetch is a modern, promise-based way to make HTTP requests. It is more straightforward and supports more features compared to XMLHttpRequest.

fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => console.log('Data:', data))
.catch(error => console.error('There was a problem with the fetch operation:', error));

3. Using axios

axios is a popular library for making HTTP requests. It is promise-based and provides an easy-to-use API.

First, you need to install it:

bash
npm install axios

Then, you can use it as follows:

const axios = require('axios');
axios.get('https://api.example.com/data')
.then(response => {
console.log('Data:', response.data);
})
.catch(error => {
console.error('Error:', error);
});

4. Using jQuery

jQuery provides a simplified way to make AJAX requests. It is useful if you are already using jQuery in your project.

$.ajax({
url: 'https://api.example.com/data',
method: 'GET',
success: function (data) {
console.log('Data:', data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('Error:', textStatus, errorThrown);
}
});

5. Using async/await with fetch

Using async/await with fetch can make the code more readable and maintainable.

async function fetchData() {
try {
let response = await fetch('https://api.example.com/data');
if (!response.ok) {
throw new Error('Network response was not ok');
}
let data = await response.json();
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();

These methods cover different scenarios and preferences for making API calls in JavaScript, from using native browser APIs to leveraging external libraries for added features and simplicity.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Pooja Mishra
Pooja Mishra

Written by Pooja Mishra

🌱 Educator 💻 Programmer 🌐 Full Stack Developer 🔥 Motivator 📘 Content creator 🧨 AI 🔥 Machine Learning 👋 ReactJS 🐍 Python ⬆️ Node JS 📈 Entrepreneurship

Responses (1)

Write a response