three.js 本地运行项目报错Access to script at 'file:...'from origin 'null' 跨域

810 阅读1分钟

Access to script at 'file:///D:/VScode/Review/three.js-master/build/three.module.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

译文: 来自原点的“空”已被CORS策略阻止:跨源请求只支持协议方案:http,数据,chrome, chrome-extension, https。

跨域

why

  • 因为你是在本地打开的文件,而file协议并不支持
  • http, data, chrome, chrome-extension, https这些协议是支持跨域请求的
  • 而当你在某盘符位置下直接打开一个网页(script标签中引入了某地的某个js文件),则在浏览器地址栏呈现如下:file:///D:/MyStudyProject/JSTopLevel/chapter-9/2%E5%AF%BB%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E5%B8%A6%E5%BC%95%E5%8F%B7%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2.html,则会出现跨域问题。而http、https等协议支持跨域请求。

解决办法

所以解决办法就是通过搭建本地一个服务器去进行资源的问题来解决跨域问题

原文链接blog.csdn.net/qq_44163269…