解决Access to XMLHttpRequest at ‘file:///E:/WebGIS/ex02/js/local.json’ 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-server
node.js安装成功后
(如何安装node.js)
1.Win +r ,输入cmd
2.输入
npm i -g http-server
3.运行成功如图
二和三是并列关系,是通过两种方法运行,选择一种即可。
二、运行http-server
在cmd中cd至index.html的文件夹
输入http-server
在浏览器中输入
localhost:8080,即实现自动跳转
或者复制上一步中弹出的http……,进入即可。
若直接双击index.html打开,则依旧报错
三、通过file运行index.html
1.用vscode打开index.html
2.安装插件
Debugger for Chrome
3.运行-启动调试-chrome
会出现launch.js的文件
在此中输入:
"file": "${workspaceFolder}/index.html"
一般输入file之后就会自动补全
4.在终端输入
npm install -g live-server
5.运行成功后输入
live-server
6.此时如果出错,则在开始的搜索栏中搜索:
并以管理员身份运行
7.打开了powershell命令行之后,输入
set-ExecutionPolicy RemoteSigned
然后更改权限为A
最后通过
get-ExecutionPolicy
查看当前的状态
出现remotesigned即为成功
8.回到vscode,再次运行
live-server
9以后直接在终端输入
live-server
即可
会自动打开网页,成功运行
此时F12,会发现控制台没有运行报错了