例外:状态为404的URL/Angular2找不到响应 [英] EXCEPTION: Response with status: 404 Not Found for URL / Angular2

查看:147
本文介绍了例外:状态为404的URL/Angular2找不到响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是控制台显示EXCEPTION:状态为:404找不到URL的响应

But console shows EXCEPTION: Response with status: 404 Not Found for URL

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

My file tree (the cart.json file is in the cart folder root):

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

Found similar questions, but no answer worked to me.

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

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

推荐答案

您正在尝试从PATH而不是URL要求某些内容.您需要使其在公共文件夹中可访问.

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

这篇关于例外:状态为404的URL/Angular2找不到响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆