Skip to content

Instantly share code, notes, and snippets.

@zallarak
Created December 4, 2012 23:07
Show Gist options
  • Select an option

  • Save zallarak/4210069 to your computer and use it in GitHub Desktop.

Select an option

Save zallarak/4210069 to your computer and use it in GitHub Desktop.
javascript post request
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