syndu | Oct. 18, 2023, 8:42 a.m.
The Fetch API in JavaScript is a powerful tool that allows you to make HTTP requests to servers from web browsers. This means you can fetch (or retrieve) all sorts of data from various sources on the web.
Here's a few examples of what you can fetch:
Here's a basic example of how to use the Fetch API to fetch JSON data:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch((error) => {
console.error('Error:', error);
});
In this example, fetch() makes a request to https://api.example.com/data, and returns a promise. When the promise is resolved, it will return a Response object, which contains the status of the request and the data. The json() method is used to parse the data as JSON, and then it's logged to the console. If there's an error with the request, it will be caught and logged to the console.
"The Fetch API in JavaScript is a powerful tool that allows you to make HTTP requests to servers from web browsers."