Answers for "fetch web api"

35

js fetch 'post' json

//Obj of data to send in future like a dummyDb
const data = { username: 'example' };

//POST request with body equal on data in JSON format
fetch('https://example.com/profile', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
.then((response) => response.json())
//Then with the data from the response in JSON...
.then((data) => {
  console.log('Success:', data);
})
//Then with the error genereted...
.catch((error) => {
  console.error('Error:', error);
});

//																		Yeah
Posted by: Guest on March-28-2020
8

fetch api javascript

fetch('https://apiYouWantToFetch.com/players') // returns a promise
  .then(response => response.json()) // converting promise to JSON
  .then(players => console.log(players)) // console.log to view the response
Posted by: Guest on September-01-2020
1

fetch api

// Error handling while fetching API

const url = "http://dummy.restapiexample.com/api/v1/employee/40";
fetch(url) //404 error
     .then( res => {
          if (res.ok) {
                return res.json( );
          } else {
                return Promise.reject(res.status); 
           }
      })
      .then(res => console.log(res))
      .catch(err => console.log('Error with message: ${err}') );
Posted by: Guest on November-27-2020
25

xml http request fetch

fetch('my/url/').then(respone()=>{
		console.log(response);
}
Posted by: Guest on September-16-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language