【JavaScript:编译错误】Image from origin 'file://' has been blocked from loading by Cross-Origin Resource

问题描述:

Image from origin 'file://' has been blocked from loading by Cross-Origin Resource Sharing policy: Received an invalid response. Origin 'null' is therefore not allowed access.

中文翻译:来自“file://”的图像已被跨源资源共享策略阻止加载:收到无效响应。 因此不允许原点'null'访问。

 

当时直接用chrome浏览器打开three.js中的example中的 .html文件。【比如webgl_materials_texture_anisotropy.html】

正常的:

断点:

three.js  中34444 行: var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );

html中117行:var texture1 = textureLoader.load( "textures/crate.gif" );

html中124行:var texture2 = textureLoader.load( "textures/crate.gif" );

发现可能是“异步加载”的原因,导致html中加载的函数都运行好一段时间后才报错,并且除了部分没显示,其他内容都正常显示了。

 

问题分析:

可能还是JavaScript限制了读取本地文件的问题。

 

解决方法:

  1、在chrome快捷方式中的属性中添加上--allow-file-access-from-files 这个命令行参数。

  但是这个方法是不一定好,必须手动修改快捷方式的内容。

  不知道是否可以通过代码设置来处理。这样不需要客户都修改自己的浏览器参数。

  而且这个读取本地文件的方法是否会应生出其他不安全的隐患?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The CORS (Cross-Origin Resource Sharing) policy is a security measure implemented by web browsers to restrict access to resources (such as APIs) on a different origin (domain, protocol, or port) than the one the web page is served from. In your case, it seems that you are making a request from 'http://localhost:8081' to a different origin, and the server is blocking that request due to CORS policy restrictions. To resolve this issue, you can try the following solutions: 1. Enable CORS on the server-side: If you have control over the server-side code, you can configure the server to include the appropriate CORS headers in the response. These headers allow the browser to determine if it should allow the request. The minimal required headers are 'Access-Control-Allow-Origin' and 'Access-Control-Allow-Methods'. You can set the value of 'Access-Control-Allow-Origin' to '*' to allow requests from any origin during development. However, in production, it's recommended to set it to the specific origin of your application. 2. Use a proxy server: If you don't have control over the server-side code or enabling CORS is not feasible, you can set up a proxy server that acts as an intermediary between your client-side code and the server. Your client-side code will make requests to the proxy server, and the proxy server will forward those requests to the actual server. Since the proxy server will be on the same origin as your client-side code, you won't face any CORS issues. 3. Disable CORS in the browser: Although not recommended for production environments, during development, you can disable CORS checks in your browser. Keep in mind that this is a temporary solution and should not be used in a production environment. The method to disable CORS varies between browsers, so you need to search for instructions specific to your browser. Remember to consider security implications when implementing any solution and ensure that proper CORS configurations are applied in a production environment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值