window.location.href与POST而不是GET(或等效的效果) [英] window.location.href with POST instead of GET (or equivalent effect)
问题描述
我想通过POST以与使用HTML 表格
相同的方式向其提供新参数后重新加载网页,但是来自JavaScript(在HTML页面内,但在表单
的上下文之外。
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 form
but from within JavaScript (inside an HTML page, but outside the context of a form
).
这可能是HTTP POST而不是GET请求(种类 XMLHttpRequest
加上替换当前显示的文档)?如果必须使用 XMLHttpRequest
(而不是 window.location.href
),我该如何替换文档?第二个问题已部分回答此处。
Is this possible as HTTP POST instead of GET request (kind of XMLHttpRequest
plus replace the currently shown document)? How could I replace the document, if XMLHttpRequest
must be employed (instead of window.location.href
)? The second question has been partially answered here.
推荐答案
我一直这样做(用jquery)是这样的。
The way I have always done this (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();
这篇关于window.location.href与POST而不是GET(或等效的效果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!