JavaScript Fetching External Data

The fetch API

The Fetch API provides us with an interface for fetching resources (including external data through the network). The main fetch method returns a Promise that resolves to a Response regardless of the status code the server returned in the response.

The fetch method takes one mandatory parameter: the path of the resource we want to fetch. We can optionally specify a second parameter that customizes the behavior of the Request.

Read more on MDN

Fetch API

Code example

Here we are using the official Wikipedia API to search for Norway.

const path = "https://en.wikipedia.org/w/api.php?origin=*&action=opensearch&search=Norway"
fetch(path)
.then(response => response.json())
.then(console.log)