DEV Community

Anitha vedamoorthy
Anitha vedamoorthy

Posted on

axios and fetch in javascript

1)what is axios
It is open source library for making the HTTP request to the server.It Handling the error better than the fetch()
It automatically transforming the request in to json formate.

Example

axios.get("https://fakestoreapi.com/products"")
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
How to install the axios
In linux

CODE

  • npm install axios

2) Fetch() function in java script
The fetch() API in JavaScript provides a promise-based way to make network requests, such as fetching data from a server or submitting form data.It offers a more flexible and powerful alternative to the older XMLHttpRequest (XHR) method

Example

fetch("https://fakestoreapi.com/products")
.then((res)=> res.json())
.then(json response)=>console.log(json response)
.catch((rej)=>rej.json())

Fetch function arguments
In arguments there are two state one is mandatory and another one is optional.The fetch() function have a one mandatory argument as in URL.The URL of the resource to fetch, and returns a Promise that resolves a Response object or else it will reject

Top comments (0)