DEV Community

Ranjani R
Ranjani R

Posted on

JS-FETCH API

Today I am going to discuss about the Fetch API which is the most powerful and modern way to handle HTML requests.

The Fetch API is a built-in JavaScript interface that allows you to make network requests similar to XMLHttpRequest, but with a cleaner, promise-based syntax. It’s widely used for fetching resources like JSON data from APIs, submitting form data, or uploading files.

It is mainly used because:

  1. Replaces the more complex XML requests.
  2. Uses Promises to make async operations which is more effective. 3.Works in both browser and worker contexts.
Syntax:

fetch(url, options)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Enter fullscreen mode Exit fullscreen mode

The most common HTTP methods are

GET Retrieve data
POST Add new data to server
PUT Update existing data
DELETE Remove existing data from server

Fetch supports Cross-Origin Resource Sharing (CORS), allowing requests to external domains if the server permits it.

That's all about the Fetch API....see you all in the next post.

Top comments (0)