[英]Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data
I am getting this error in Chrome and Opera Browsers: 我在Chrome和Opera浏览器中收到此错误:
Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
The code is working fine in Internet Explorer, Mozilla Firefox, and Safari. 代码在Internet Explorer,Mozilla Firefox和Safari中运行良好。 But I need to fix it in Chrome and Opera.
但我需要在Chrome和Opera中修复它。 Please help me to find a solution to fix this problem?
请帮我找一个解决方案来解决这个问题?
I am getting this error at this line 我在这一行得到了这个错误
imgData = ctx.getImageData(x1,y1,w,h);
May be this will help, as you have mentioned cross origin so try this, 可能这会有所帮助,因为你已经提到了交叉起源所以试试这个,
var UimageObj = new Image();
crossOrigin has to be set to enable the canvas data to be saved.The source image should have access-control-allow-origin
set to *
or a chosen domain 必须设置crossOrigin以保存画布数据。源图像应将
access-control-allow-origin
设置为*
或选定的域
UimageObj.crossOrigin = 'anonymous'; // crossOrigin attribute has to be set before setting src.If reversed, it wont work.
UimageObj.src = obj_data.srcUser;
Hope it helps. 希望能帮助到你。
When you load your html file from disk using: file://path/to/your/file.html
, then Google Chrome and Opera will raise error at line including: imgData = ctx.getImageData(x1,y1,w,h);
使用:
file://path/to/your/file.html
从磁盘加载html文件时,谷歌Chrome和Opera将引发错误,包括: imgData = ctx.getImageData(x1,y1,w,h);
Solution is simple: start web server (apache, nginx) put your html file somewhere at web server and load your html file from: http://localhost/somewhere/file.html
解决方案很简单:启动Web服务器(apache,nginx)将您的html文件放在Web服务器的某处并从以下位置加载您的html文件:
http://localhost/somewhere/file.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.