我正在尝试从Angular将数据发布到后端Node.js文件。在表单提交后,以下函数被调用:
cmdSubmit() : void
{
console.log(this.cli);
this.Cliservice.postdata(this.cli)
.then(clidata => this.clidata.push(clidata),
error => this.errorMessage = <any>error);
}
postdata函数的代码:
postdata(obj: any): Promise<any> {
alert('hello');
let body = JSON.stringify({ obj });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.runUrl, body, options)
.map(this.extractData)
.catch(this.handleError);
/*return this.http.post(this.runUrl, body, options)
.map(this.extractData)
.catch(res => {
// do something
// To throw another error, use Observable.throw
return Observable.throw(res.json());
});*/
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
但是这里什么也没有发生。我没有收到任何错误信息。你能否提供一些建议?
Observable
和Promise
,而没有转换为Promise
。 - Maximilian Riegler