从HTTP远程服务器获取Angular 2的JSON失败,但在本地成功 [英] Getting JSON for Angular 2 from HTTP remote server fails, but succeeds localy
问题描述
我正在尝试使用Angular2中的htttp.get读取JSON文件.
I'm trying to read a JSON file using htttp.get in Angular2.
当我在项目中使用存储在 localy 中的文件时,工作正常.
When I use a file stored localy on my project, it works OK.
但是-当我使用远程服务器,并且完全相同的文件时,出现以下错误:
But - when I use a remote server, with exactly the same file, I get the following error:
404-未找到集合未定义"未找到
浏览到http://example.com/my.json
,我可以在任何浏览器上看到该文件.
Browsing to http://example.com/my.json
, I can see the file on any browser.
这是我获取文件的打字稿代码:
Here is my typescript code for getting the file:
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
.您需要从项目中删除与此项目相关的所有内容,这等同于从NgModule
中的imports
中删除以下内容:
Not found collection
error suggest that you have used angular-in-memory-web-api
before. You need to remove everything related to that from your project, that would equal removing from imports
in your NgModule
the equal of the following:
InMemoryWebApiModule.forRoot(InMemoryDataService)
以便您可以使用外部api和db.
so that you are able to use external api and db.
否则,您的代码不应更改,但是如果您愿意,请遵循 本教程 应为您提供帮助.
Otherwise your code shouldn't change, but if you want, following this tutorial should help you.
这篇关于从HTTP远程服务器获取Angular 2的JSON失败,但在本地成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!