JSFiddle - React, Tailwind, and code Playground

by jeanlescure

HTML

<button onclick="downloadCsv()">
  Download CSV
</button>

JavaScript

var data = {
  "some": "fake",
  "data": true
};

function downloadCsv() {
  var theForm = document.createElement('form');
  document.body.appendChild(theForm);

  theForm.setAttribute('method', 'POST');
  theForm.setAttribute('action', 'https://www.mocky.io/v2/5d488be63300003b56a3ef86');
  theForm.setAttribute('target', 'theWindow');
  
  Object.keys(data).forEach(function (k) {
    var newInput = document.createElement('input');
    newInput.setAttribute('type', 'hidden');
    newInput.setAttribute('name', k);
    newInput.value = data[k]; 
    theForm.append(newInput);
  });
  
  var theWindow = window.open('', 'theWindow', 'width=500,height=100');

  theForm.submit();
}