Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 609
Koby Douek Avatar asked Dec 11 '22 12:12

Koby Douek


1 Answers

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.

like image 194
AT82 Avatar answered Dec 13 '22 22:12

AT82