Last active
December 27, 2015 23:29
-
-
Save aswebdev/d8859f0471e9e9133e5d to your computer and use it in GitHub Desktop.
JavaScript function to POST object data to a 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
// Post to Page Function | |
function postToPage(url,data) { | |
// Setup Form Object | |
var f = document.createElement("form"); | |
f.setAttribute('method',"post"); | |
f.setAttribute('id',"formPost"); | |
f.setAttribute('action',url); | |
// Loop Data | |
for (var key in data) { | |
var obj = data[key]; | |
var i = document.createElement("input"); //input element, text | |
i.setAttribute('type',"hidden"); | |
i.setAttribute('name',key); | |
i.setAttribute('value',obj); | |
f.appendChild(i); | |
} | |
document.getElementsByTagName('body')[0].appendChild(f); // Append to Page | |
document.getElementById("formPost").submit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment