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

Logo

前往低代码交流专区

更多推荐