1

I am creating an application in angular 2. I am trying to access the json data via http in a service. But i am getting an error saying that

**GET http://localhost:4200/data/products.json 404 (Not Found)**

I have the data in the specified folder but i am unable to access it.

My service code is as follows.

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import { Item } from './item';

@Injectable()
export class ItemService {
observableItems: Observable<Item[]>
allItems: Item[] = [];
selectedItems: Item[] = [];
errorMessage: string;
url = "http://localhost:4200/data/products.json";
constructor(private http:Http) { 
   this.observableItems = this.http.get(this.url).map((res: Response) => res.json());
   this.observableItems.subscribe(
             data => this.allItems = data,
         error =>  this.errorMessage = <any>error);
}
getItems(): Observable<Item[]> {
   return this.observableItems;
}
getSelectedItems(): Item[] {
   return this.selectedItems;
}   
    addItem(id:number): void {
       let item = this.allItems.find(ob => ob.id === id);
       if (this.selectedItems.indexOf(item) < 0) {     
      this.selectedItems.push(item);
   }
    }
    removeItem(id:number): void {
   let item = this.selectedItems.find(ob => ob.id === id);
   let itemIndex = this.selectedItems.indexOf(item);
       this.selectedItems.splice(itemIndex, 1);
    }
} 

enter image description here

1
  • You probably want to access an endpoint serving data in JSON format. An address http://localhost:4200/data/products.json suggests that there is a document product.json available for download. You are more likely to query http://localhost:4200/data/products. Commented Jul 6, 2017 at 11:51

2 Answers 2

2

My html files are in

"Project/src/app/..."

So to access JSON File I need to come back from tabs folder and app folder to reach the base directory for both code and images. I used ../ to come back from a folder.

From this my url in the service will be as follows:

url = "../../assets/data/products.json";
Sign up to request clarification or add additional context in comments.

Comments

0

Try using direct path to the file from the location of your service file. For example:

url = "data/products.json";

It will work.

7 Comments

can you please tell me the path of your file relevant to the service? It should work.
i tried changing the path of the file into the src.. the path is url="./assets/data/data.json"
can you share the screenshot of your folder structure where i can see the service and assets folder?
Have attached the screenshot. have a look into it
Ok the path you should use is "../assets/data/data.json". Try and let me know if it resolves the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.