Fetch API in JavaScript
The Fetch API provides a simple way to make HTTP requests and handle responses in JavaScript.
Making a GET Request
To fetch data from a server, you can use fetch() to make a GET request:
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
Activity
Try It Yourself!
Use the Fetch API to retrieve data from a different public API and log the results to the console.
Quiz
Quick Quiz
- What is the Fetch API used for?
- What method is used to parse the response body as JSON?
- What happens if there is an error during the fetch process?
Answers: Making HTTP requests; response.json(); the catch method handles errors.