POST Action with custom adapter


#1

Hi everybody! This is my adapter for make a post action on the server. I’ve got some troubles to make that! Am i missing something?

import DS from ‘ember-data’;

export default DS.Adapter.extend({

defaultSerializer: ‘report’,

findAll(store, type, sinceToken){ return new Promise(function(resolve, reject) { let report = store.peekAll(‘report’); resolve(report); }) },

createRecord(store, type, snapshot){

let data = this.serialize(snapshot, { includeId: true });
alert(data);

return new Promise(function(resolve,reject){
  let xhr = new XMLHttpRequest();
  var method = 'POST';
  var url = 'https://test.vigifarmaco.it/api/v1/submit';
  var headers = {
    "Vigifarmaco-Api-Key" : "0Utrr0yHwHQbXr9TT1COrQtt",
    "Content-type" : "text/xml"
  };

  if ("withCredentials" in xhr) {
  // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }

  if (!xhr) {
    alert('CORS not supported');
    return;
  }


  xhr.onreadystatechange = alertContents;
  xhr.setRequestHeader("Vigifarmaco-Api-Key", "0Utrr0yHwHQbXr9TT1COrQtt");
  xhr.setRequestHeader("Content-type" , "text/xml");
  xhr.send();

  function alertContents() {
    if (xhr.readyState == 4){
      if(xhr.status == 200){
        resolve(this.responseXML);
      }
      else {
      reject(new Error('sendData: `' + url + '` failed with status: [' + this.status + ']'+ 'failed with readyState:' + this.readyState+''));
      }
    }
  };

});

}

});


#2

Think of Adapters as a mechanism of telling the very opinionated Ember Data how to call your API. But let Ember Data make the call for you. Look at the urlFor… family of methods.