Window.location.href with POST instead of GET (or equivalent effect)
I would like to reload a web page after providing new parameters to it via POST in the same way as it would be possible with HTML form
, but from within JavaScript (inside the HTML page, but out of context form
).
Is it possible as an HTTP POST instead of a GET request (kind XMLHttpRequest
plus replace the current document)? How can I replace the document if XMLHttpRequest
should be used (instead of window.location.href
)? The second question is partially answered here .
+3
source to share
1 answer
The way I've always done it (with jquery) is this.
var $form=$(document.createElement('form')).css({display:'none'}).attr("method","POST").attr("action","URLHERE");
var $input=$(document.createElement('input')).attr('name','FIRST NAME HERE').val("FIRST VALUE HERE");
var $input2=$(document.createElemet('input')).attr('name','SECOND NAME HERE').val("SECOND VALUE HERE");
$form.append($input).append($input2);
$("body").append($form);
$form.submit();
+6
source to share