iframe跨端口报错 Blocked a frame with origin from accessing a cross-origin frame
前言
在不同的端口号,甚至是不同的ip进行iframe嵌套的时候,在父页面调用子页面的方法的时候,报错
SecurityError: Blocked a frame with origin from accessing a cross-origin frame…
问题原因
在不同端口号下,不能使用传统的iframe嵌套调用方法。
document.getElementById("mainFrame").contentWindow.xxxx();
因为
同源安全策略
你不能用javascript访问一个<iframe>,如果你能做到这一点,那将是一个巨大的安全缺陷。对于同一源策略浏览器,阻止脚本尝试访问具有不同源的帧。
如果地址中至少有一个部分未保留,则认为来源不同:
<protocol>://<hostname>:<port>/path/to/page.html
如果要访问iframe,协议、主机名和端口必须与域相同。
Examples
下面是尝试访问以下URL时将发生的情况
http://www.example.com/home/index.html
URL RESULT
http://www.example.com/home/other.html -> Success
http://www.example.com/dir/inner/another.php -> Success
http://www.example.com:80 -> Success (default port for HTTP)
http://www.example.com:2251 -> Failure: different port
http://data.example.com/dir/other.html -> Failure: different hostname
https://www.example.com/home/index.html.html -> Failure: different protocol
ftp://www.example.com:21 -> Failure: different protocol & port
https://google.com/search?q=james+bond -> Failure: different hostname & protocol
解决方法
尽管同一源站策略阻止脚本访问不同源站的内容,但如果您同时拥有这两个页面,则可以使用window.postmessageand其相关消息事件在两个页面之间发送消息来解决此问题。
//在主页面
var frame = document.getElementById('your-frame-id');
frame.contentWindow. postMessage (data, '*'); data可以是string,boolean,number,array,object //在iframe子页面
window. addEventListener ('message', function(event) {
//event.data获取传过来的数据
});
注意:父页面的postMessage是触发addEventListener的
iframe跨端口报错 Blocked a frame with origin from accessing a cross-origin frame的更多相关文章
- iframe跨源报错:"Blocked a frame with origin from accessing a cross-origin frame"
一.报错信息: “Blocked a frame with origin from accessing a cross-origin frame” 二.在stackoverflow上找到原因 Same ...
- 端口报错listen eaddrinuse:::xxx
端口报错 listen eaddrinuse:::xxx 表示这个端口被占用 结束正在使用此端的程序即可.
- canvas.toDataURL 由于跨域报错的解决方法
关于canvas.toDataURL 由于跨域报错的解决方法 用过canvas,都知道toDataURL这个方法真好用,不仅合成图片用到它,压缩图片也用到它.但有一个问题,就是图片源不能跨域,不然会报 ...
- ie9/8的iframe中jQuery报错
此文章用于对工作中遇到的问题进行记录 jQuery 版本:1.9.1 按照一般的思路,jquery 1.x的是支持ie9及以下的,但是今天发现jquery报错了,代码错误位置在源码版本的第4888行 ...
- Access to Image at 'file:///Users canvas本地图片跨域报错解决方案
1.设置跨域 添加跨域条件 crossorigin="anonymous" 前提是后端支持这个图片跨域 2.上面加了之后还是报错 如标题所示 你需要把你的项目放到服务器上面跑 ...
- 远程测试mysql数据库3306端口报错
错误现象:[root@localhost ~]# telnet 192.168.10.130 3306Trying 192.168.10.130...Connected to 192.168.10.1 ...
- django 在centos 7 下 指定ip地址和端口 报错问题
windows environment: python manage.py runserver host:port centos environment: python manage.py runse ...
- ElasticSearch6 报错blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];
原文连接:https://blog.csdn.net/u013042707/article/details/84110531 方案:curl -XPUT -H "Content-Type: ...
- CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie
1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...
随机推荐
- 利用dnslog进行sql注入
更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 能够利用dnslog进行注入的两个关键点:secure_file_priv 不能为NULL,仅支持 Windows但不支持 Linux s ...
- .net core 多sdk 多版本 环境切换
在讲述.net core多版本之前,我们先理解一下.net core sdk与.net core runtime之前的联系与区别,根据官网的解释我们可以简单地理解为:sdk是在开发过程中进行使用,而r ...
- 微信小程序路由带参
通过url带参传递 wxml <navigator url="../user/user?id={{item.id}}&name={{item.name}}">点 ...
- Gift to XBACK(小小礼物)
什么白天 什么黑夜 我没有 准备着给你的 Surprise 你给我的爱 让我觉得已足够 是你让我相信爱会有 是你的爱陪我绕宇宙 打开日记本写下忧愁 你却让我看时间轴 我才知道现在我能看到的画面 拥有你 ...
- 洛谷 P2918 [USACO08NOV]买干草Buying Hay 题解
P2918 [USACO08NOV]买干草Buying Hay 题目描述 Farmer John is running out of supplies and needs to purchase H ...
- centos7中运行ifconfig提示-bash: ifconfig: command not found
centos7中运行ifconfig提示-bash: ifconfig: command not found 查看/sbin/下是否有ifconfig,若没有通过如下命令安装 sudo yum ins ...
- Hyperspectral Image Classification Using Similarity Measurements-Based Deep Recurrent Neural Networks
用RNN来做像素分类,输入是一系列相近的像素,长度人为指定为l,相近是利用像素相似度或是范围相似度得到的,计算个欧氏距离或是SAM. 数据是两个高光谱数据 1.Pavia University,Ref ...
- 前端base64加密
一.Base64编码表 码值 字符 码值 字符 码值 字符 码值 字符 0 A 16 Q 32 g 48 w 1 B 17 R 33 h 49 x 2 C 18 S 34 i 50 y 3 D 19 ...
- Luogu3379 【模板】最近公共祖先(LCA)
题面 题解 这里讲一种硬核做法. 首先\(\mathrm{dfs}\)整棵树,求出这棵树的欧拉序,然后\(\mathrm{LCA}\)问题就变成了\(\pm 1\mathrm{RMQ}\)问题. 考虑 ...
- Jmeter(四十一)_图片爬虫
今天教大家用元件组合,做一个网页图片爬虫. 需要用到的元件:循环控制器+计数器+xpath提前器+函数嵌套+beanshell代码 首先我们确定一下要爬取的图片网站:https://dp.pconli ...