Html 如何用 POST 替换 window.open(...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17793183/
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
How to replace window.open(...) with a POST
提问by corsiKa
I currently have some code that runs a window.open(urlWithGetParams)
line. As far as I'm aware, this is going to force me to use a GET
request. I would like to do this with a POST request. Is there a workaround for this?
我目前有一些运行window.open(urlWithGetParams)
一行的代码。据我所知,这将迫使我使用GET
请求。我想用一个 POST 请求来做到这一点。有解决方法吗?
I'm not married to window.open()
, either. I'm open to just about any alternative that allows me to spawn a new window via a POST request instead of a GET.
我也没有嫁给window.open()
。我对任何允许我通过 POST 请求而不是 GET 生成新窗口的替代方案持开放态度。
回答by Denys Séguret
In fact I made a small "library" for this, open in POST a new window :
事实上,我为此做了一个小“库”,在 POST 中打开一个新窗口:
// Arguments :
// verb : 'GET'|'POST'
// target : an optional opening target (a name, or "_blank"), defaults to "_self"
window.io = {
open: function(verb, url, data, target){
var form = document.createElement("form");
form.action = url;
form.method = verb;
form.target = target || "_self";
if (data) {
for (var key in data) {
var input = document.createElement("textarea");
input.name = key;
input.value = typeof data[key] === "object"
? JSON.stringify(data[key])
: data[key];
form.appendChild(input);
}
}
form.style.display = 'none';
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
};
Example :
例子 :
io.open('POST', 'fileServer.jsp', {request: {key:"42", cols:[2, 3, 34]}});
To open in a new window, set the target
parameter :
要在新窗口中打开,请设置target
参数:
io.open('POST', someURL, someArgs, 'newwin');
or to ensure it's a new window/tab each time :
或确保每次都是一个新窗口/标签:
io.open('POST', someURL, someArgs, '_blank');
回答by Naftali aka Neal
What I do is that I do a javascript AJAX post and then I take the content that I get back and place it into a new window.
我所做的是我做一个 javascript AJAX 帖子,然后我把我得到的内容放到一个新窗口中。
Something like this (using jQuery, but you can use any AJAX implementation):
像这样(使用 jQuery,但您可以使用任何 AJAX 实现):
$.post(URL, DATA, function(d){
var new_window = window.open();
$(new_window.document.body).append(d);
});