get data from fetch response code example
Example 1: js fetch api
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
});
return response.json();
}
postData('https://example.com/answer', { answer: 42 })
.then(data => {
console.log(data);
});
Example 2: .fetch method
fetch('http://example.com/data.json')
.then(data => data);
.catch(err => console.log(err));
Example 3: work with fetches in js
function yourfunction() {
return fetch(API)
.then(response => response.json())
.then(data => data.content)
}