fetch request allow cors
// From http://foo.com/
fetch('http://bar.com/data.json', {
mode: 'no-cors' // 'cors' by default
}).then(function(response) {
// Do something with response
});
fetch request allow cors
// From http://foo.com/
fetch('http://bar.com/data.json', {
mode: 'no-cors' // 'cors' by default
}).then(function(response) {
// Do something with response
});
Add no cores to fetch
// There were no quick access to mode and credentials to other fetch answers.
// Data you'll be sending
const data = { funny: "Absolutely not", educational: "yas" }
fetch('https://example.com/api/', {
method: 'POST', // The method
mode: 'no-cors', // It can be no-cors, cors, same-origin
credentials: 'same-origin', // It can be include, same-origin, omit
headers: {
'Content-Type': 'application/json', // Your headers
},
body: JSON.stringify(data),
}).then(returnedData => {
// Do whatever with returnedData
}).catch(err => {
// In case it errors.
})
fetch suntax
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
fetch get request
// Example POST method implementation:
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
postData('https://example.com/answer', { answer: 42 })
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us