Created
March 6, 2017 13:17
-
-
Save eduardoluizgs/0a22ec55e06d1a88dd18a88896b0ba51 to your computer and use it in GitHub Desktop.
JavaScript Post Form
This file contains 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 post_to_url(path, params, method) { | |
method = method || "post"; | |
var form = document.createElement("form"); | |
//Move the submit function to another variable | |
//so that it doesn't get overwritten. | |
form._submit_function_ = form.submit; | |
form.setAttribute("method", method); | |
form.setAttribute("action", path); | |
for(var key in params) { | |
var 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_function_(); //Call the renamed function. | |
} | |
// Works! | |
post_to_url("http://url", {"key": "value"} ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment