javascript window.location.href 使用 POST 而不是 GET(或等效效果)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26697338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:20:22  来源:igfitidea点击:

window.location.href with POST instead of GET (or equivalent effect)

javascriptresthttp

提问by Drux

I'd like to reload a web page after supplying new parameters to it via POST in the same way as would be possible with an HTML formbut from within JavaScript (inside an HTML page, but outside the context of a form).

我想在通过 POST 以与 HTML 相同的方式form但从 JavaScript 内部(在 HTML 页面内部,但在 a 的上下文之外form)提供新参数后重新加载网页。

Is this possible as HTTP POST instead of GET request (kind of XMLHttpRequestplus replace the currently shown document)? How could I replace the document, if XMLHttpRequestmust be employed (instead of window.location.href)? The second question has been partially answered here.

这是否可能作为 HTTP POST 而不是 GET 请求(有点XMLHttpRequest加上替换当前显示的文档)?如果XMLHttpRequest必须使用(而不是window.location.href),我该如何更换文件?第二个问题在这里得到了部分回答。

回答by MarshallOfSound

The way I have always done this (with jquery) is this.

我一直这样做(使用jquery)的方式是这样的。

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();