方案一、本地web server(官网推荐,一次性处理)
官网提供了几种方式( local webserver):
python的内置模块:
-
python3环境运行python -m http.server
-
python2环境运行python -m SimpleHTTPServer
emrun
emrun --no_browser --port 8080 hello.html
以上命令都是在命令行中执行
方案二、修改浏览器配置(永久解决)
关于同源策略问题,可以参考一下方式直接处理,而不用本地起web server,以下内容转载自浏览器不能加载本地资源:
Chrome(没有成功):
1、
(1)得到Chrome的安装路径,例如:C:\Program Files\Google\Chrome\Application
(2)在命令行窗口,输入安装路径,加上–allow-file-access-from-files参数,例如:
Chrome installation path\chrome.exe --allow-file-access-from-files
重新启动浏览器即可。
2、在Chrome的快捷方式,右键->属性->目标的文本框中加上参数 --allow-file-access-from-files
例:“C:\Program Files \Google\Chrome\Application\chrome.exe” --allow-file-access-from-files
Firefox(亲测有效):
1、在浏览器的地址栏输入“about:config”,回车
2、在过滤器(filter)中搜索“security.fileuri.strict_origin_policy”
3、将security.fileuri.strict_origin_policy设置为false
4、关闭目前开启的所有Firefox窗口,然后重新启动Firefox。