Fetch Javascript
2 min
Por Juliano Alves
Using Fetch in JavaScript
The fetch
API is a modern way to make network requests in JavaScript. It provides a more powerful and flexible feature set than the older XMLHttpRequest
. 😆
Basic Usage
To make a simple GET request, you can use the following syntax:
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);
})
.catch((error) => {
console.error('There was a problem with the fetch operation:', error);
});