2017-05-17 37 views
0

我试图发布一个对象到json服务器,但我得到了500内部服务器错误。以下是我的代码。POST http:// localhost:3000/claimdetails 500(内部服务器错误)

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

组件

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

+0

难道我的回答可以帮助您?一些反馈会很好 – Dinistro

+1

嘿Dinistro,你的答案确实有很大的意义。感谢您的帮助=) –

回答

0

您使用JSON.stringify()两次。当你将它传递给createService()时,你的json变量已经是JSON

我只会保留在createService()之内。

注: 上observables不要使用.toPromise()。你可以做同样的observables,无需promises

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

然后订阅你的组件

相关问题