Heim  >  Artikel  >  Web-Frontend  >  js使用post 方式打开新窗口_javascript技巧

js使用post 方式打开新窗口_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:12:471433Durchsuche

js中一般新打开窗口很简单直接window.open(url);就可以了,

但是由于我希望传递参数到服务端,而且参数看起来很长一串,而且get方式的提交参数长度是有限制的,因此我有以下需求:

1,js中实现post提交

2,返回的页面在新窗口显示

首先我是这么做的:

复制代码 代码如下:

 $.ajax({  
                     type: "POST",  
                     url: '${contextPath}/analyse/detail.do',  
                     data: {carNum :carNum,ids:refIds},  
                     success: function(str_response) { var obj = window.open("about:blank");  
                        obj.document.write(str_response);  
                     }  
                 });

通过jQuery ajax提交,返回的数据写在新的页面中,但是由于浏览器的会拦截自动弹出的窗口,这样还需用户自己解除拦截,用户体验很差,

然后我又通过模拟form表单的提交来实现

复制代码 代码如下:

function post(URL, PARAMS) { var temp_form = document.createElement("form");     
            temp_form .action = URL;     
            temp_form .target = "_blank";
            temp_form .method = "post";     
            temp_form .style.display = "none"; for (var x in PARAMS) { var opt = document.createElement("textarea");     
                opt.name = x;     
                opt.value = PARAMS[x];     
                temp_form .appendChild(opt);     
            }     
            document.body.appendChild(temp);     
            temp_form .submit();    
        }

注意:如需新打开窗口 form 的target属性要设置为'_blank'

然后请求post('${contextPath}/analyse/detail.do',{carNum :carNum,ids:refIds});就可以了

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn