When was the fetch api put into javascript? code example

Example 1: 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.
})

Example 2: fetch post json

(async () => {
  const rawResponse = await fetch('https://httpbin.org/post', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({a: 1, b: 'Textual content'})
  });
  const content = await rawResponse.json();

  console.log(content);
})();