js fecth get
The Fetch API is a modern web API that provides an interface for fetching resources (such as JSON data, HTML pages, images, etc.) from a server. It is an alternative to the older XMLHttpRequest (XHR) API.
Here is an example of using the Fetch API to perform a GET request:
fetch('https://example.com/data.json')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
In this example, we are fetching JSON data from the URL 'https://example.com/data.json'. The fetch() function returns a Promise that resolves to the Response object representing the server's response. We then use the json() method of the Response object to extract the JSON data from the response, which returns another Promise that resolves to the actual data. Finally, we log the data to the console.
The catch() method is used to handle any errors that may occur during the fetch request or parsing of the response.
原文地址: https://www.cveoy.top/t/topic/bfMb 著作权归作者所有。请勿转载和采集!