xxxxxxxxxx
fetch('https://api.example.com/data')
.then(response => {
// Check if the response status is in the range 200-299 (successful response)
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Parse the response body as JSON and return it as a promise
return response.json();
})
.then(data => {
// Work with the data received in the response
console.log(data);
})
.catch(error => {
// Handle errors that may occur during the fetch or JSON parsing
console.error('Fetch error:', error);
});