javascript 同源图像 texImage2D 的安全错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25291959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SecurityError for same-origin image texImage2D
提问by vcapra1
I am currently learning WebGL. In a call to texImage2D, which is called when a texture has finished loading, I get the following SecurityError
:
我目前正在学习 WebGL。在对 texImage2D 的调用中,当纹理完成加载时调用,我得到以下信息SecurityError
:
Uncaught SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at /path/to/texure.png may not be loaded.
However, the file is on the same domain, in fact it is in the same directory as the html file requesting it.
但是,该文件在同一个域中,实际上它与请求它的 html 文件在同一目录中。
Here is the file layout:
这是文件布局:
> js
|-> script.js
|-> glUtils.js
|-> sylvester.js
> texture.png
> index.html
And when I look in the F12 console's resource list, the image texture.png
is there, fully loaded, and it is 256 x 256. Why does it think I am requesting from another domain?
当我查看 F12 控制台的资源列表时,图像texture.png
在那里,完全加载,并且是 256 x 256。为什么它认为我正在从另一个域请求?
采纳答案by vcapra1
The solution is that you must be on a local webserver, because file:/// domain requests will not be granted. (Information given by Pointy:
解决方案是您必须在本地网络服务器上,因为 file:/// 域请求将不会被授予。(Pointy 提供的信息:
If you serve up the stuff from a local webserver it'll work, because the browser will see those as being from the same domain. Chrome used to have an command-line option to allow it: --allow-file-access-from-files but I don't know if it still works
如果您从本地网络服务器提供内容,它将起作用,因为浏览器会将这些内容视为来自同一域。Chrome 曾经有一个命令行选项来允许它: --allow-file-access-from-files 但我不知道它是否仍然有效
回答by gman
You're probably getting the error because you're using a file://
based URL.
您可能会收到错误消息,因为您使用的是file://
基于 URL 的 URL。
The solution is to use a simple web server. Open a terminal and type
解决方案是使用一个简单的 Web 服务器。打开终端并输入
python -m SimpleHTTPServer
then go to http://localhost:8000
(install python if you're on Windows) or use node.jsor possibly even better use devd
然后去http://localhost:8000
(如果你在 Windows 上安装 python)或使用node.js或者甚至可能更好地使用devd
Do NOT use --allow-file-access-from-files
. This opens your machine to getting hacked through the browser. That would be like turning off your firewall or your virus scanner.
不要使用--allow-file-access-from-files
。这将打开您的机器以通过浏览器被黑客入侵。这就像关闭防火墙或病毒扫描程序一样。