JavaScript fetch API

It is an API in JavaScript that let you make network requests and return the response as a Promise object.

Below is the example that shows how to make a GET request to an API endpoint and log the response:

Syntax:

In this example, the fetch function is called with the mentioned URL . The fetch function returns a Promise, which is passed to the then method to wait for the response. The response.json method is used to parse the response as JSON data. In this example then method is used to log the data when it becomes available. The catch method is used to catch any errors that occur during the network request.

Below example make POST requests and send data with the fetch function. Below is an example:

In above example, the fetch function is called with the URL of the API endpoint and an options object. The options object specifies the method as "POST" request. Also it sets the headers and body properties to send data in request.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top