如何用POST替换window.open(...) [英] How to replace window.open(...) with a POST
问题描述
我目前有一些运行 window.open(urlWithGetParams)
行的代码。据我所知,这将迫使我使用 GET
请求。我想用POST请求来做到这一点。有没有解决这个问题的方法?
我还没有和 window.open()
结婚。我对任何允许我通过POST请求而不是GET产生新窗口的替代方法都是开放的。
//参数:
//动词:'GET'|'POST'
// target:可选打开目标(名称或_blank),默认为_self
open = function ,url,data,target){
var form = document.createElement(form);
form.action = url;
form.method = verb;
form.target = target || _自;
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();
}; b
$ b示例: code> open('POST','fileServer.jsp',{request:{key:42,cols:[2,3,34]}});
要在新窗口中打开,请设置目标
参数:
open('POST',someURL,someArgs,'newwin');
或确保每次都是新的窗口/标签:
open('POST',someURL,someArgs,'_blank');
很明显,您应该给它一个不同于打开
,这样你就不会影响现有的。理想情况下,您应该命名空间。
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?
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.
In fact I made a small "library" just for this, open in POST a new window :
// Arguments :
// verb : 'GET'|'POST'
// target : an optional opening target (a name, or "_blank"), defaults to "_self"
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();
};
Example :
open('POST', 'fileServer.jsp', {request: {key:"42", cols:[2, 3, 34]}});
To open in a new window, set the target
parameter :
open('POST', someURL, someArgs, 'newwin');
or to ensure it's a new window/tab each time :
open('POST', someURL, someArgs, '_blank');
Obviously you should give it a name other than open
so that you don't shadow the existing one. Ideally you should namespace it.
这篇关于如何用POST替换window.open(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!