SecurityError for same-origin image texImage2D

不想你离开。 提交于 2019-12-12 15:42:06

问题


I am currently learning WebGL. In a call to texImage2D, which is called when a texture has finished loading, I get the following 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.

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?


回答1:


The solution is that you must be on a local webserver, because file:/// domain requests will not be granted. (Information given by 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




回答2:


You're probably getting the error because you're using a file:// based URL.

The solution is to use a simple web server. Open a terminal and type

python -m SimpleHTTPServer

then go to http://localhost:8000 (install python if you're on Windows) or use node.js or possibly even better use 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.



来源:https://stackoverflow.com/questions/25291959/securityerror-for-same-origin-image-teximage2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!