Alexandr Belov
Alexandr Belov

Reputation: 1834

EXCEPTION: Response with status: 404 Not Found for URL / Angular2

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')

Upvotes: 1

Views: 2169

Answers (1)

wesside
wesside

Reputation: 5740

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

Upvotes: 1

Related Questions