EXCEPTION:状态响应:找不到404 / Angular2

时间:2017-02-01 21:09:48

标签: json http angular

我正在尝试在我的服务中获得http响应:

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()
      );
  }
}

但控制台显示EXCEPTION:响应状态:找不到404 404 enter image description here

我的文件树( cart.json 文件位于购物车文件夹根目录中): enter image description here

发现了类似的问题,但没有答案对我有用。

UPD cart.json 文件移至公开文件夹,将路径更改为缩短的文件解决了这个问题。

return this.http.get('../cart.json')

1 个答案:

答案 0 :(得分:1)

您正在尝试从PATH中获取某些内容,而不是URL。您需要在公共文件夹中访问它。