Access to XMLHttpRequest at 'file://.....' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
對於file
協議來說,瀏覽器是不支持跨域的。當然有很多方法可以搭建一個本地服務器。如果只是簡單寫一個html
文件,不想去配置其他東西。以下方法只適合於vscode
。
1、安裝debugger for chrome插件
2、創建json文件
在安裝好插件後,點擊側邊欄中的run
,在點擊create a launch json file
就會在當前目錄下創建文件夾,內部有個json文件。
就可以對url進行配置,比如說你想要展示目錄下的example.html
這個文件。
只需更改webRoot
選項即可
"webRoot": "${workspaceFolder}/example.html"
3、安裝live-server
全局安裝live-server
npm i live-server -g
4、啓動
在根目錄下執行
live-server
即可