使用ES6模块化import本地js文件出现跨域问题的解决方式
Access to script at ‘file:///C:/Users/Administrator/Desktop/Vue/11-%E6%A8%A1%E5%9D%97%E5%8C%96%E5%BC%80%E5%8F%91/02-ES6%E6%A8%A1%E5%9D%97%E5%8C%96/aaaa.js’ from origin ‘null’ has been blocked by CORS
·
Access to script at ‘file:///C:/Users/Administrator/Desktop/Vue/11-%E6%A8%A1%E5%9D%97%E5%8C%96%E5%BC%80%E5%8F%91/02-ES6%E6%A8%A1%E5%9D%97%E5%8C%96/aaaa.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.
翻译内容得知,跨源请求只支持协议方案:http,数据,chrome, chrome-extension, https。
为什么会出现跨域
- 因为你是在本地打开的文件,而file协议并不支持
- http, data, chrome, chrome-extension, https这些协议是支持跨域请求的
- 而当你在某盘符位置下直接打开一个网页(script标签中引入了某地的某个js文件),则在浏览器地址栏呈现如下:file:///D:/MyStudyProject/JSTopLevel/chapter
- 所以解决办法就是通过搭建本地一个服务器去进行资源的问题来解决跨域问题。
如何解决跨域
1. 在 vscode中安装 Live Server 插件
2.在对应.html文件点击右键选择Open with Live Server
更多推荐
已为社区贡献1条内容
所有评论(0)