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')
3
  • 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. Commented Feb 1, 2017 at 21:11
  • I am just learning, and was doing everything according to this tutorial: youtube.com/watch?v=IOp9OmNdHy4 Commented Feb 1, 2017 at 21:13
  • I'd recommend running through the real tutorial, which shows how to move from a local file to actually needing HTTP angular.io/docs/ts/latest/tutorial Commented Feb 1, 2017 at 21:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.