2.0.0 • Published 5 months ago

getapidata v2.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Fetch Data Script

Dit is een eenvoudig JavaScript-script dat gegevens ophaalt van een JSON-bestand of een API met behulp van de fetch API. Hier lees je hoe je het kunt gebruiken.

Installatie

Kopieer de functie fetchData uit het script en plak deze in je JavaScript-bestand.

// Reusable function to fetch data from JSON or API
function fetchData(url) {
  return fetch(url)
    .then((response) => {
      if (!response.ok) {
        throw new Error(`Network response was not ok: ${response.statusText}`);
      }
      return response.json();
    })
    .catch((error) => {
      console.error("Error fetching data:", error);
      throw error; // Rethrow the error to handle it at a higher level
    });
}
// Voorbeeldgebruik:
const jsonUrl = "https://example.com/data.json";
const apiUrl = "https://api.example.com/data";

// Gegevens ophalen vanuit JSON-bestand
fetchData(jsonUrl)
  .then((data) => {
    console.log("Data van JSON-bestand:", data);
    // Behandel de gegevens zoals nodig
  })
  .catch((error) => {
    // Behandel fouten
  });

// Gegevens ophalen vanuit API
fetchData(apiUrl)
  .then((data) => {
    console.log("Data van API:", data);
    // Behandel de gegevens zoals nodig
  })
  .catch((error) => {
    // Behandel fouten
  });
2.0.0

5 months ago

1.0.0

5 months ago