Skip to content

Instantly share code, notes, and snippets.

@pokka
Created August 10, 2016 02:41
Show Gist options
  • Save pokka/6a15b9ffde46e63b299e13e235197fcf to your computer and use it in GitHub Desktop.
Save pokka/6a15b9ffde46e63b299e13e235197fcf to your computer and use it in GitHub Desktop.
javascript es6 post data via form
function postForm(path, params = {}) {
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', path);
Object.keys(params).forEach((key) => {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
});
document.body.appendChild(form);
form.submit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment