Created
October 1, 2015 10:48
-
-
Save adfinlay/9ed0fe3875bc078e533f to your computer and use it in GitHub Desktop.
Create and send a POST request with form data
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
postRequest = function(path, parameters) { | |
var form = $('<form></form>'); | |
form.attr("method", "post"); | |
form.attr("action", path); | |
var addField = function(key, value) { | |
var field = $('<input></input>'); | |
field.attr("type", "hidden"); | |
field.attr("name", key); | |
field.attr("value", value); | |
form.append(field); | |
}; | |
$.each(parameters, function(key, value) { | |
if (parameters.hasOwnProperty(key)) { | |
if( value instanceof Array ){ | |
for(var i = 0; i < value.length; i++){ | |
addField(key + "[]", value[i]) | |
} | |
} | |
else{ | |
addField(key, value); | |
} | |
} | |
}); | |
$(document.body).append(form); | |
form.submit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment