使用iframe进行传值时,如果采用URL的方式传递,则最大只能传递大约2k的数据,超过这个长度就会被截断了,那么是否可以采用post的方式传值呢?答案是可以的,我们可以采用js创建一个form表单,并将此form提交到iframe上,这样就能做到打破2k数据的限制。
<iframe id="frame1" name="frame1" src="" style="display:none"></iframe>
<script>
var tranVal = function(){
document.getElementById("frame1").style.display = "block";
var postForm = document.createElement("form");
postForm.method = "post";
postForm.action = "http://localhost:8080/test/getdata.jsp";
postForm.target = "frame1";//用来将数据提交到frame1上
var input = document.createElement("input");
input.type = "hidden";
input.name = "username";//这个是传值的名称,要记住
var strA = "1234567890";
//循环500000次后,保证字符串的大小大于2M
for(var i=0;i<500000;i++) strA+= "1234567890";
input.value = strA;
postForm.appendChild(input);
document.body.appendChild(postForm);
postForm.submit();
//用完之后,不要忘记删掉
postForm.removeChild(input);
document.body.removeChild(postForm);
}
</script>
<button onclick="tranVal()">
传递值
</button>
还需要注意2个问题:
1 tomcat下post不能超过2M的问题
这个需要将tomcat的配置文件server.xml修改一下:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" maxPostSize="0"/>
maxPostSize为0时,表示不用限制长度。
2 跨域的问题
跨域不影响iframe的post传值,发送方的tomcat无需更改,只是需要将接收方的tomcat的maxPostSize修改为0即可。
分享到:
相关推荐
框架完美解决了iframe之间的跨域通讯。底层技术采用window.name转换代理实现
iframe 跨域访问session问题解决方法
该文档介绍了vue和普通web页面中iframe实现跨域的解决方案,解决了主页面中无法调用iframe方法的问题
iframe跨域问题:Uncaught DOMException Blocked a frame with origin解决方法
NULL 博文链接:https://hqlly.iteye.com/blog/1662337
这是关于iframe使用过程中出现的问题整理的解决方法,关于使用iframe不用单独写接口打通数据,直接把数据通过ifarme嵌套方法传递过去,使用简单方便。
以前在面试的时候经常遇到问关于跨域的事儿,所以自己对跨域有一定的概念性了解,知道什么是跨域以及解决跨域的方法,但是具体实际从来没有操作过,直到最近在公司项目中,遇到了一个需要使iframe跨域进行POST提交的...
【ASP.NET编程知识】iframe跨域与session失效问题的解决办法.docx
主要介绍了iframe跨域与session失效问题的解决办法,有需要的朋友可以参考一下
iframe父向子传值实例完美解决跨域问题,遇到此问题的小伙伴快下载解决问题吧! gogogogogo!
通过HTTPClient界面在JSP中嵌入iframe子界面跨域时,无法获取跨域界面的属性值的问题
iframe 跨域 自动适应高度;iframe 跨域 自动适应高度;
在web开发中,跨域问题是经常遇到的,但是由于浏览器同源策略的限制,不同域之间属性和操作是无法直接交互的。本次讨论iframe和父页面的消息通信。
iframe跨域访问示例
iframe 与主框架相互访问例子,包含同域访问,跨域访问例子。
var c=a.document.documentElement,d=b.left-2+c.scrollLeft,e=b.top-2+c.scrollTop;try{var g=a.ownerDocument.parentWindow.frameElement||null;if(g){var f=2-(g.frameBorder||1)*2;d+=f;e+=f}}catch(h){}return ...
layer弹窗:top.layer弹窗到父页面跨域,通过postMessage方法将子页面的配置对象发送到父页面中,父页面再通过子页面的配置对象打开弹窗,对象内不能存在事件。
iframe子向父传值实例完美解决跨域问题,遇到此问题的小伙伴快下载解决问题吧! gogogogogo!
iframe跨域,里面包含四个文件,具体使用方式参考代码,