这个问题在这里已经有了答案:
Disable same origin policy in Chrome
(35 个回答)
2年前关闭。
它在 console
中显示以下消息.
sandbox.js:1 Fetch API cannot load file:///C:/Users/Lizzi/Desktop/novice%20to%20ninja/todos/luigi.json. URL scheme must be "http" or "https" for CORS request
我是
aynchronouse
的新手编程,但我已阅读 CORS
解决方案并尝试了诸如获取 chrome 扩展程序并为我的 google chrome 禁用网络安全,但它仍然无法正常工作。fetch('todos/luigi.json').then((response)=>{
console.log('resolved',response);
}).catch((err)=>{
console.log('rejected', err);
});
最佳答案
您需要在本地为您的 index.html 提供服务,或者将您的网站托管在某处的实时服务器上,以便 Fetch API 正常工作。需要使用 http 或 https 协议(protocol)来提供文件。
如果您只是从文件资源管理器中单击 index.html,那么您的浏览器会直接从文件系统中获取这些文件。这就是错误向您显示计算机上根文件夹的绝对路径的原因。
尝试安装其中之一...
- npm 服务
- 实时服务器(Visual Studio Code 的扩展,如果你正在使用它)
或者任何适用于您的环境的服务器。
启动服务器后应该可以正常工作:)快乐编码!
关于javascript - 如何解决错误 "URL scheme must be "http"或 CORS 请求的 "https""。对于此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58128248/