Javascript
Javascript로 Post Submit 하기
SaintsP
2020. 7. 16. 12:42
/*** sends a request to the specified url from a form. this will change the window location.* @param {string} path the path to send the post request to* @param {object} params the paramiters to add to the url* @param {string} [method=post] the method to use on the form*/function post(path, params, method='post') {// The rest of this code assumes you are not using a library.// It can be made less wordy if you use one.const form = document.createElement('form');form.method = method;form.action = path;for (const key in params) {if (params.hasOwnProperty(key)) {const hiddenField = document.createElement('input');hiddenField.type = 'hidden';hiddenField.name = key;hiddenField.value = params[key];form.appendChild(hiddenField);}}document.body.appendChild(form);form.submit();}post('/contact/', {name: 'Johnny Bravo'});