0

我一直在订阅,如何从邮政服务中获取数据返回。即使我尝试在我的应用程序组件中进行转换,我也无法做到。

设施服务.ts

saveFacility(facility) { 
    const headers = new Headers({'Content-Type': 'application/json'});
    return this.http.post('http://localhost:8000/saveFacility', 
facility, headers)
    .subscribe(
    (response: Response) => {
    return response.json();
},
(error) => {
return error.json();
}
);
}

设施组件.ts

savePatient() {
const response = 
this.facilityService.saveFacility(this.facilityForm.value)
console.log(response);
}

我正在获取 sbscription 对象,但我想以 json 的形式返回。

4

1 回答 1

3

RxJS 库提供了 Observable 运算符,您可以使用它们来操作正在发出的数据,例如 map、filter。

尝试使用地图运算符:

    saveFacility(facility) { 
        const headers = new Headers({'Content-Type': 'application/json'});
        return this.http.post('http://localhost:8000/saveFacility', facility, headers)
            .map(response => response.json())
            .subscribe((response: Response) => {
                    return response.json();
                },
                (error) => {
                    return error.json();
                }
            );
    }
于 2017-12-11T14:14:39.737 回答