Fetch API - Como usar

// Example POST method implementation:

postData('http://example.com/answer', {answer: 42})
.then(data => console.log(data)) // JSON from `response.json()` call
.catch(error => console.error(error))

function postData(url, data) {
// Default options are marked with *
return fetch(url, {
body: JSON.stringify(data), // must match 'Content-Type' header
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *omit
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
method: 'POST', // *GET, PUT, DELETE, etc.
mode: 'cors', // no-cors, *same-origin
redirect: 'follow', // *manual, error
referrer: 'no-referrer', // *client
})
.then(response => response.json()) // parses response to JSON
}

 

Comentários

Postagens mais visitadas deste blog

Instalação NetBeans