我正在尝试使用 Angular2 中的 htttp.get 读取 JSON 文件。
当我使用存储的文件时 本地 在我的项目中,运行正常 .
但是 - 当我使用 时远程服务器 , 与 正好同一个文件,我收到以下错误:
404 - Not Found Collection 'undefined' not found
浏览到
http://example.com/my.json
,我可以在任何浏览器上看到该文件。这是我获取文件的 typescript 代码:
private url = '../my.json'; <--- This works
private url = 'http://example.com/my.json'; <--- This throws the error
return this.http.get(this.url)
.map(this.extractData)
.catch(this.handleError);
提前致谢。
最佳答案
Not found collection
错误提示您使用过angular-in-memory-web-api
前。您需要从项目中删除与此相关的所有内容,这相当于从 imports
中删除。在您的 NgModule
等于以下各项:
InMemoryWebApiModule.forRoot(InMemoryDataService)
这样您就可以使用外部 api 和 db。
否则您的代码不应更改,但如果您愿意,请关注 this tutorial应该可以帮助你。
关于angular - 从 HTTP 远程服务器获取 Angular 2 的 JSON 失败,但在本地成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386685/