使用 ng-include
包含 html 文件时出现以下错误。
XMLHttpRequest cannot load file:///home/algo/Dnyaneshwar/Angular/TestProject/views/header1.html.
Cross origin requests are only supported for protocol schemes:
http, data, chrome, chrome-extension, https, chrome-extension-resource.
最佳答案
问题原因
发生此错误是因为您只是尝试直接从浏览器打开 html 文档。由于您使用的是 file://或 C:/,这就是为什么错误表明它们不是 http://或 https://。
不要尝试直接通过文件夹运行它。
如何解决
您需要通过网络服务器访问您的代码并在本地主机上访问它。如果您安装了 eclipse 或 apache,请使用它来访问您的文件。有些IDE(如intellij、eclipse)内置了Web服务器。
如果你有节点设置,那么只需运行npm install http-server -g
,你就可以在终端中使用它,比如http-server C:\path\to\app
另请参阅
关于angularjs - 使用 ng-include 包含 html 文件时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951023/