【发布时间】:2017-11-24 10:16:46
【问题描述】:
所以我正在制作一个服务器,它将基于使用 http 从 json 文件中读取产品
问题是当我构建应用程序时出现此错误 加载资源失败:服务器响应状态为 404(未找到)
这是我的服务代码 product.service.ts
import { Injectable } from '@angular/core';
import {Http, Response} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import { IProduct } from './product/produit';
@Injectable()
export class ProductdataService {
private _productUrl:string='./product/products.json'
constructor(private _http:Http) {
}
getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
.map((response : Response)=><IProduct[]> response.json())
.do(data => console.log('All: ' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error:Response)
{console.error(error);
return Observable.throw(error.json().error || 'Server Error')
}
}
这是我的 json 文件:
[{
"idpr":1,
"productName": "The Alchemist",
"description":"Paulo Coelho's enchanting novel has inspired a devoted following around the world." ,
"imgUrl":"https://images-na.ssl-images-amazon.com/images/I/41MeC94AxIL._SX324_BO1,204,203,200_.jpg" ,
"price":10.5,
"starRating":3,
"author":"Paulo Coelho",
"category":"Litterature"
}
,{
"idpr":2,
"productName": "Cry From The Grave",
"description":"After a heartbreaking tragedy six years ago, Hannah Walker is struggling to pick up the pieces of her life – until a chance discovery tips her whole world upside down again. " ,
"imgUrl":"https://images-eu.ssl-images-amazon.com/images/I/61ehavtz8SL._SY346_.jpg" ,
"price":5.25,
"starRating":3,
"author":" Carolyn Mahony",
"category":"Thriller"
},{
"idpr":3,
"productName": "You are the Best Wife",
"description":"Ajay believes in living for himself; Bhavna teaches him to live for others. Ajay is a planner for life Bhavna makes him live in every moment." ,
"imgUrl":"https://images-eu.ssl-images-amazon.com/images/I/51QELCohn6L.jpg" ,
"price":3.5,
"starRating":3,
"author":" Ajay Pandey ",
"category":"Romance"
},
{
"idpr":4,
"productName": "Dr. Jekyll and Mr. Hyde",
"description":"Mr. UTTERSON the lawyer was a man of a rugged countenance that was never lighted by a smile" ,
"imgUrl":"https://ia801606.us.archive.org/zipview.php?zip=/26/items/olcovers36/olcovers36-L.zip&file=368462-L.jpg" ,
"price":3.48,
"starRating":3,
"author":" Robert Louis Stevenson",
"category":"Science"
}
]
这是我放置 json 文件的位置,因此服务中的 url 将是:'./product/products.json' enter image description here
请帮忙
【问题讨论】: