manniru
4/29/2018 - 6:51 AM

es6_fetch_post.js

test1 = () => {
    fetch('https://realtime-system.firebaseio.com/nbbf/test/.json')
      .then(function (response) {
        return response.json();
      })
      .then(function (myJson) {
        console.log('test1='+myJson);
      });
  }

  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, same-origin, *omit
      headers: {
        'user-agent': 'Mozilla/4.0 MDN Example',
        'content-type': 'application/json'
      },
      method: 'POST', // *GET, POST, PUT, DELETE, etc.
      mode: 'cors', // no-cors, cors, *same-origin
      redirect: 'follow', // manual, *follow, error
      referrer: 'no-referrer', // *client, no-referrer
    })
    .then(response => response.json()) // parses response to JSON
  }
  
  
  this.postData('https://realtime-system.firebaseio.com/nbbf/test2/.json', {answer: 42})
      .then(data => console.log(data)) // JSON from `response.json()` call
      .catch(error => console.error(error))