Created
April 22, 2021 17:27
-
-
Save yanli0303/9026ad3e5a223856f277ad637d07e453 to your computer and use it in GitHub Desktop.
Open a new browser window using HTTP POST method.
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
/** | |
* Submit login form with POST method. | |
* @param {string} action The login page URL. | |
* @param {string} target The name of the iframe, or `_blank`; | |
* @param {object} params The dictionary of form data. | |
* @returns The form element inserted into the page. | |
*/ | |
export const postForm = (action, target, params) => { | |
const form = document.createElement('form') | |
form.style.display = 'none' | |
form.setAttribute('method', 'post') | |
form.setAttribute('action', action) | |
form.setAttribute('target', target) | |
Object.entries(params).forEach(([key, value]) => { | |
const input = document.createElement('input') | |
input.setAttribute('type', 'text') | |
input.setAttribute('name', key) | |
input.value = value | |
form.appendChild(input) | |
}) | |
document.body.appendChild(form) | |
form.submit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment