所以我制作的服务器将基于使用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 / product.json' enter image description here
plzz帮助
答案 0 :(得分:0)
该json文件不会包含在构建的应用程序中。它需要位于代码的编译输出所在的文件夹中(或者如果使用angular-cli,则可以将其添加到assets文件夹中)