问题
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