javascript – 同源图像texImage2D的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.

但是,该文件位于同一个域中,实际上它与请求它的html文件位于同一目录中.

这是文件布局:

> js
|-> script.js
|-> glUtils.js
|-> sylvester.js
> texture.png
> index.html

当我查看F12控制台的资源列表时,图像texture.png就在那里,完全加载,它是256 x 256.为什么它认为我从另一个域请求?

解决方法:

解决方案是您必须在本地Web服务器上,因为不会授予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

上一篇:javascript – three.js:无法使用THREE.OBJLoader显示obj文件


下一篇:关于Unity发布的WebGL项目出现问题的解决方法和本地网站部署