2017-11-24 2 views
-1
gefunden

so im ein Server zu machen, die aus einer JSON-Datei auf Lese Produkte werden s Gonna basierend httpMeine JSON-Datei URL ist nicht in meinem Winkel Server

das Problem ist, dass, wenn ich die Anwendung erstellen Dieser Fehler erscheint fehlgeschlagen Ressource laden: der Server mit einem Status von 404 geantwortet (nicht gefunden)

dies ist mein Servicecode 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') 
 

 
    } 
 

 
}

und das ist meine json Datei:

[{ 
 
    "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" 
 
      
 
     } 
 
    ]

und das, wo ich meine JSON-Datei platziert, so dass die URL im Dienst sein wird:‘./product/products.json ' enter image description here

plzz Hilfe

Antwort

0

die jSON-Datei wird nicht in der integrierten App enthalten sein. Es muss sich in dem Ordner befinden, in dem die kompilierte Ausgabe Ihres Codes ist (oder Sie können es zum Ordner "Assets" hinzufügen, wenn Sie angle-cli verwenden)

+0

es funktioniert dank einem Loooot –

Verwandte Themen