1

I am trying to get an http response in my service:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';

import 'rxjs/add/operator/map';

@Injectable()
export class CartService {
    constructor(private http: Http){ }

getCartItems() {
    return this.http.get('./cart/cart.json')
    .map(
        (res) => res.json()
      );
  }
}

But console shows EXCEPTION: Response with status: 404 Not Found for URL enter image description here

My file tree (the cart.json file is in the cart folder root): enter image description here

Found similar questions, but no answer worked to me.

UPD Moving the cart.json file into public folder and changing path to a shortened one solved the problem.

return this.http.get('../cart.json')
Alexandr Belov
  • 1,804
  • 9
  • 30
  • 43
  • Why are you using an HTTP call to just get the content of a file elsewhere in the same application? That doesn't make any sense. – jonrsharpe Feb 01 '17 at 21:11
  • I am just learning, and was doing everything according to this tutorial: https://www.youtube.com/watch?v=IOp9OmNdHy4 – Alexandr Belov Feb 01 '17 at 21:13
  • I'd recommend running through the real tutorial, which shows how to move from a local file to actually needing HTTP https://angular.io/docs/ts/latest/tutorial/ – jonrsharpe Feb 01 '17 at 21:14

1 Answers1

1

You're trying to require something from a PATH, rather then a URL. You need to make it accessible in your public folder.

wesside
  • 5,622
  • 5
  • 30
  • 35