W3Schools JavaScript: Mastering GET Requests

Sarah Jul 13, 2026

Diving into JavaScript's rich set of features, one powerful ability is its capability to interact with external APIs and fetch data - all made feasible with the Fetch API or XHR (XMLHttpRequest). Today, we'll take a closer look at utilizing JavaScript's get request to access and manipulate data from remote sources. Let's explore this functionality through practical examples and demonstrate how it can enhance your web applications.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Before we dive in, let's ensure we have a fundamental understanding of what a Get Request is. In essence, a get request is a method used to obtain data from a server. This method sends a request to a server — in HTTP, a GET request is issued to retrieve information from the server. The server then responds with the requested data. Now, let's get our hands dirty with JavaScript!

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

Sending a Simple Get Request with JavaScript

JavaScript can send get requests asynchronously using the Fetch API or the older XMLHttpRequest method. Let's start with the Fetch API, introduced in 2015, offering a more powerful and flexible way to retrieve resources from the network.

JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡

Here's a basic example of using Fetch API to send a GET request:

```javascript fetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(data => console.log(data)); ```

Async/Await Syntax with Fetch API

Javascript Cheet cheet
Javascript Cheet cheet

To make our code more readable and manageable, we can use async/await, a modern JavaScript syntax that allows writing async code in a more traditional, sequential manner. Here's the same get request, re-written with async/await:

```javascript async function fetchPosts() { const response = await fetch('https://jsonplaceholder.typicode.com/posts'); const data = await response.json(); console.log(data); } fetchPosts(); ```

Handling Errors in Get Requests

While fetching data, errors may occur due to network issues, the server returning an unexpected status code, etc. It's crucial to handle these error cases. Let's update our async/await example to include error handling:

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

```javascript async function fetchPosts() { try { const response = await fetch('https://jsonplaceholder.typicode.com/posts'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); console.log(data); } catch (error) { console.log('Error:', error); } } fetchPosts(); ```

Using XMLHttpRequest to Send Get Requests

Before the introduction of the Fetch API, XMLHttpRequest (XHR) was the primary way to send HTTP requests, including GET requests, in JavaScript.

Here's how you can send a GET request using XHR:

JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳

```javascript const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts'); xhr.responseType = 'json'; xhr.onload = function() { if (xhr.status === 200) { const data = xhr.response; console.log(data); } }; xhr.send(); ```

Async/Await with XMLHttpRequest

While it's not as common, it's possible to use async/await with XHR by using a Promise constructor, like this:

an info sheet with different types of web pages
an info sheet with different types of web pages
Grading System In Javascript With Source Code
Grading System In Javascript With Source Code
the web programming poster is shown
the web programming poster is shown
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
the web programming poster is shown
the web programming poster is shown
Javascript String Methods
Javascript String Methods
JavaScript Recursion Simplified
JavaScript Recursion Simplified
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
JavaScript study guide for fast learning
JavaScript study guide for fast learning
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code
To-Do App Project using JavaScript ⚡
To-Do App Project using JavaScript ⚡
add active class in javascript
add active class in javascript
JavaScript calculator projet
JavaScript calculator projet
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
CSS Notes for Beginners (Easy + Quick Guide) 🎨
CSS Notes for Beginners (Easy + Quick Guide) 🎨
Javascript setTimeout() method
Javascript setTimeout() method
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
How to Create a Function in JavaScript: Step-by-Step Guide 🚀
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript

```javascript function fetchPostsAsync() { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts'); xhr.responseType = 'json'; xhr.onload = function() { if (xhr.status === 200) { resolve(xhr.response); } else { reject(new Error(`Error ${xhr.status}: ${xhr.statusText}`)); } }; xhr.onerror = function() { reject(new Error('Network error')); }; xhr.send(); }); } async function run() { try { const data = await fetchPostsAsync(); console.log(data); } catch (error) { console.log(error); } } run(); ```

This article has demonstrated how to send GET requests using the Fetch API and XMLHttpRequest in JavaScript, including how to handle errors and use async/await syntax. Now you have the tools to start fetching data and enhancing your web applications' functionality!