Created
August 10, 2016 02:41
-
-
Save pokka/6a15b9ffde46e63b299e13e235197fcf to your computer and use it in GitHub Desktop.
javascript es6 post data via form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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