2017-05-17 3 views
0

Ich versuche, ein Objekt in json Server zu posten, aber ich habe den 500 internen Server Fehler. Unten sind meine Codes.POST http: // localhost: 3000/claimdetails 500 (Interner Serverfehler)

app.service:

createService(url: string, param: any): Promise<any> { 
let body = JSON.stringify(param); 
return this.http 
    .post(url, body, this.options) 
    .toPromise() 
    .then(this.extractData) 
    .catch(this.handleError);} 

Komponente:

save() { 
var json = JSON.stringify({ 
    purpose: this.purpose 
}); 

this.appService 
    .createService('http://localhost:3000/claimdetails', json) 
    .then(result => console.log("5. createService: " + result)) 
    .catch(error => console.log(error));} 

ERROR: error

+0

Habe meine Antwort geholfen? Einige Rückmeldungen wären toll – Dinistro

+1

Hey Dinistro, deine Antwort hat viel Sinn ergeben. Danke für deine Hilfe =) –

Antwort

0

verwendet Sie JSON.stringify() zweimal. Ihre json-Variable ist bereits JSON, wenn Sie sie in createService() übergeben.

Ich würde es nur in der createService() halten.

HINWEIS: Verwenden Sie nicht .toPromise() auf observables. Sie können mit observables, keine Notwendigkeit für promises das gleiche tun:

createService(url: string, param: any): Observable<any> { 
    let body = JSON.stringify(param); 
    return this.http 
     .post(url, body, this.options) 
     .map(this.extractData) 
     .catch(this.handleError); 
} 

Und dann in der Komponente abonnieren

Verwandte Themen