Skip to content

Instantly share code, notes, and snippets.

@hom3chuk
Created September 28, 2015 22:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hom3chuk/692bf12fe7dac2486212 to your computer and use it in GitHub Desktop.
Save hom3chuk/692bf12fe7dac2486212 to your computer and use it in GitHub Desktop.
// See https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit, jQuery version with arrays and objects support
function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
if ( typeof value == 'object' || typeof value == 'array' ){
$.each(value, function(subkey, subvalue) {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key+'[]');
field.attr("value", subvalue);
form.append(field);
});
} else {
var field = $('<input />');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
}
});
$(document.body).append(form);
form.submit();
}
@StephanWeinhold
Copy link

I'd use var post = function as every function in JS is a variable.

@GEOEGII555
Copy link

Comment by @StephanWeinhold on Jan 29, 2016
I'd use var post = function as every function in JS is a variable.

function post already creates a variable & looks better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment