How to Fetch Data from a URL Using JavaScript?

Estimated read time 1 min read

You can fetch data from a URL using JavaScript with the fetch API, like this:

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, the fetch function is used to make a GET request to the specified URL, https://api.example.com/data. The returned response is then converted to JSON format using the json method and logged to the console. In case of an error, the error is caught and logged to the console using the catch method.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply