PowerRC's Blog

Posted by:
PowerRC

码农,吃货,偶尔飙车和摄影,应该属于危险人物。

4,963

用window.open时能否通过post方式传输数据

不能的。
Window.open只能用get的方式(附带在url后面)。

那么有别的方法达到类似的效果吗?
有的。

直接构建一个临时form,然后指定form的参数method = post, target=_blank
这样,就既弹出一个新窗口,又可以把数据post过去了。
具体例子:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function exportOrderInfo()  {
   
                   
            var tempForm = document.createElement("form");  
            tempForm.id="tempForm1";  
            tempForm.method="post";  
            tempForm.action='这里填url';  
            tempForm.target='_blank';  
             
            var hideInput = document.createElement("input");  
            hideInput.type="hidden";  
            hideInput.name= "这里填数据名"
            hideInput.value= "这里填数据值";
            tempForm.appendChild(hideInput);  
   
            tempForm.appendChild(hideInput);  
            document.body.appendChild(tempForm);  
            tempForm.submit();
            document.body.removeChild(tempForm);
                   
            return true;
        }

至于其他人,window.open一个空白窗口,再利用相同的name,把数据post过去。我是没理解为何不直接post。window.open很多时候会被浏览器拦截。

版权申明

若文中未注明,则本文由 PowerRC 原创,转载请注明出处。

分享

发表评论

*

code

back up ↑