【问题标题】:EXCEPTION: Response with status: 404 Not Found for URL / Angular2例外:状态响应:404 Not Found for URL / Angular2
【发布时间】:2017-02-01 21:09:47
【问题描述】:

我正在尝试在我的服务中获得 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()
      );
  }
}

但控制台显示异常:响应状态为:404 Not Found for URL

我的文件树(cart.json 文件位于 cart 文件夹根目录中):

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

UPDcart.json 文件移动到 public 文件夹并将路径更改为缩短的路径即可解决问题。

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

【问题讨论】:

  • 你为什么使用 HTTP 调用来获取同一应用程序中其他地方的文件内容?这没有任何意义。
  • 我只是在学习,正在按照本教程做所有事情:youtube.com/watch?v=IOp9OmNdHy4
  • 我建议通过真正的教程运行,它展示了如何从本地文件移动到实际需要 HTTP angular.io/docs/ts/latest/tutorial

标签: json http angular


【解决方案1】:

您试图从 PATH 中获取某些内容,而不是从 URL 中获取。你需要让它在你的公用文件夹中可以访问。

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2020-08-05
    • 2011-01-24
    • 2014-06-22
    • 2018-06-08
    相关资源
    最近更新 更多