天天看點

跨域問題解決Access to image at 'file:///png' from origin 'null' has been blocked by CORS policy

在谷歌浏覽器中出現

Access to image at 'file:///C:/Users/DK/Desktop/threejs%E5%AD%A6%E5%A0%82/material/g8.png' 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,https網絡請求方式通路。

網上解決辦法是:crossorigin="anonymous"

以及浏覽器快捷方式加參數:chrome.exe --disable-web-security   (後患無窮)

正确的解決辦法是:建立本地伺服器,現在介紹一分鐘建立本地伺服器的辦法

方法一:Servez

簡介:https://greggman.github.io/servez/

該伺服器支援mac,linux,windows。

window版直接下載下傳exe檔案即可,有界面,選擇要建站的檔案夾,點Start 就可以了。相當簡單。

方法二:Node.js http-server

下載下傳安裝http-server

npm install --global http-server
C:\Users\mybj\AppData\Roaming\npm\hs -> C:\Users\mybj\AppData\Roaming\npm\node_modules\http-server\bin\http-server
C:\Users\mybj\AppData\Roaming\npm\http-server -> C:\Users\mybj\AppData\Roaming\npm\node_modules\http-server\bin\http-server
+ [email protected]
added 30 packages from 40 contributors in 6.6s
      

啟動http-server

指令

http-server [path] [options]      

[path]的預設值:先找 

./public

 ,否則使用 

./

 。

例如:

複制$ http-server
Starting up http-server, serving ./
Available on:
  http://192.168.17.140:8081
  http://192.168.44.1:8081
  http://192.168.208.1:8081
  http://192.168.137.1:8081
  http://127.0.0.1:8081
Hit CTRL-C to stop the server      

效果如下:

跨域問題解決Access to image at 'file:///png' from origin 'null' has been blocked by CORS policy

-----------------------------------------------------------------

  • 我做的小程式們
  • 【推薦】Web版短信管理平台源碼
  • WinForm版短信管理平台源碼
  • 移動短信程式源碼Win服務版(CMPP3.0/CMPP2.0協定)
  • 移動物聯網卡短信源碼(CMPP3.0協定,支援MsSql/MySql資料庫)
  • C#實作聯通短信Sgip協定程式源碼
  • C#實作電信短信SMGP協定程式源碼
  • C#實作移動短信CMPP服務端程式源碼
  • 小y的QQ:28657321 (歡迎交流)