JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



Web Fetch API

The Web Fetch API is a modern web API that allows developers to fetch resources from the network. It is a powerful tool that enables developers to make network requests and retrieve data from servers. The Web Fetch API is a part of the JavaScript Web APIs and is supported by all modern web browsers.

The Web Fetch API is a replacement for the older XMLHttpRequest (XHR) API. The XHR API was the standard way of making network requests in JavaScript for many years. However, the Web Fetch API is a more modern and flexible alternative that provides a simpler and more intuitive interface for making network requests.

The Web Fetch API is based on the Promise API, which is a modern JavaScript feature that allows developers to write asynchronous code in a more readable and maintainable way. The Promise API provides a way to handle asynchronous operations in a more structured and predictable way.

Using the Web Fetch API

The Web Fetch API is very easy to use. To make a network request, you simply need to create a new instance of the fetch() function and pass in the URL of the resource you want to fetch. The fetch() function returns a Promise that resolves to the response from the server.

Here is an example of how to use the Web Fetch API to fetch data from a server:

<script>
  fetch('https://jsonplaceholder.typicode.com/todos/1')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));
</script>

In this example, we are using the fetch() function to fetch data from the server. We are passing in the URL of the resource we want to fetch, which is a JSON file containing a todo item. We are then using the then() method to parse the response as JSON and log the data to the console. Finally, we are using the catch() method to handle any errors that may occur.

Fetching Data with Query Parameters

The Web Fetch API also allows you to fetch data with query parameters. Query parameters are a way to pass additional information to the server when making a network request. They are commonly used to filter, sort, or paginate data.

Here is an example of how to use query parameters with the Web Fetch API:

<script>
  fetch('https://jsonplaceholder.typicode.com/todos?userId=1')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));
</script>

In this example, we are using the fetch() function to fetch data from the server. We are passing in the URL of the resource we want to fetch, which is a JSON file containing todo items for a specific user. We are using the ?userId=1 query parameter to filter the data by user ID. We are then using the then() method to parse the response as JSON and log the data to the console. Finally, we are using the catch() method to handle any errors that may occur.

Conclusion

The Web Fetch API is a powerful and flexible tool that allows developers to fetch resources from the network. It is a modern alternative to the older XMLHttpRequest (XHR) API and is based on the Promise API, which provides a more structured and predictable way to handle asynchronous operations. The Web Fetch API is supported by all modern web browsers and is very easy to use. It allows developers to fetch data with query parameters, which is a common way to filter, sort, or paginate data.

References

Activity