Getting JSON for Angular 2 from HTTP remote server fails, but succeeds localy

随声附和 提交于 2019-11-28 10:51:37

问题


I'm trying to read a JSON file using htttp.get in Angular2.

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 - Not Found Collection 'undefined' not found

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);

Thanks in advance.


回答1:


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)

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.



来源:https://stackoverflow.com/questions/42386685/getting-json-for-angular-2-from-http-remote-server-fails-but-succeeds-localy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!