Javascript 如何在 window.open() 中发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6100247/
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-08-23 20:14:02  来源:igfitidea点击:

How to POST in window.open()

javascripthtmlpostget

提问by Victor

When we do a window.open(), is there an option to specify method = POST? Since by default, it is GET?

当我们执行 a 时window.open(),是否可以指定选项method = POST?因为默认情况下,它是GET?

What I want is this. The parent window has some form parameters (many in number) and they should be sent to the server on window.open(). It is not a good idea to append all of them in the GETurl using query string.

我想要的是这个。父窗口有一些表单参数(数量很多),它们应该在window.open(). GET使用查询字符串将所有这些附加到url 中并不是一个好主意。

回答by Pointy

You could use window.open()to open an empty window, with a name. Then you could use a <form>with a "target" attribute referring to that new window's name, and post it.

您可以使用window.open()打开一个带有名称的空窗口。然后,您可以使用<form>带有“目标”属性的 a 来引用该新窗口的名称,然后发布它。

editOK here's the idea. You have a form on the page, and it can be hidden:

编辑好这里的想法。您在页面上有一个表单,它可以隐藏:

<form id='theForm' method='post' action='/your/action' target='TheNewWindow'>
  <input type='hidden' name='param_1' value='whatever'>
</form>

Then you get the results into your window like this:

然后您将结果放入您的窗口,如下所示:

window.open('about:blank', 'TheNewWindow');
document.getElementById('theForm').submit();

Make sure that the window name you use is a valid identifier (like a JavaScript variable name), or IE will get upset.

确保您使用的窗口名称是有效的标识符(如 JavaScript 变量名称),否则 IE 会感到不安。

Hereis a jsfiddle.

是一个jsfiddle。