Created
June 25, 2014 15:45
-
-
Save nmoliveira/37a6e56a65b9875160d3 to your computer and use it in GitHub Desktop.
Javascript function post to URL
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
/** | |
* This function makes a post to a given url with the given parameters | |
* The form is created on the fly and also the input elements | |
* @path the URL path | |
* @params the URL query parameters | |
*/ | |
function postToUrl(path, params) { | |
var form = document.createElement("form"); | |
form.setAttribute("method", 'post'); | |
form.setAttribute("action", path); | |
for(var key in params) { | |
if(params.hasOwnProperty(key)) { | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment