Created
December 4, 2012 23:07
-
-
Save zallarak/4210069 to your computer and use it in GitHub Desktop.
javascript post request
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
| function postRequest(path, params, method) { | |
| method = method || 'POST'; | |
| var form = document.createElement("form"); | |
| form.setAttribute("method", method); | |
| 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); | |
| } | |
| } | |
| var hiddenField = document.createElement("input"); | |
| var csrftoken = getCookie('csrftoken'); | |
| hiddenField.setAttribute('name', 'csrfmiddlewaretoken'); | |
| hiddenField.setAttribute('value', csrftoken); | |
| 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