【发布时间】:2018-10-03 02:52:59
【问题描述】:
我有一个要求,我需要向 Web API 发出 http post 方法请求,即保存用户输入的一些数据。 Web API 将返回一些值作为结果,并且此结果将用于某些 UI 逻辑。
我尝试以同步方式发布 http 帖子,但它无法按预期正常工作。
Angular2 组件调用这个服务来保存数据:-
public SaveCubeReport(userName: any, cubeReport: CubeReportViewModel, APIServiceURL: any)
{
var result = this.SaveData(userName, cubeReport, APIServiceURL).subscribe(
data => {
console.log(data);
});
console.log(result);
}
http post方法调用如下:-
SaveData(userName: any, cubeReport: CubeReportViewModel, APIServiceURL: any)
{
var temp = this._http.post(APIServiceURL, JSON.stringify(cubeReport), { headers: ContentHeaders })
.map((res: Response) => res.json())
.do(() => {
console.log('request finished');
});
return res;
}
调用 angular2-modal 弹出窗口的父组件。
var dialog = this.modal.open(SaveCubeReport, overlayConfigFactory(
{
ReportName: this.ReportItem.rpt_name,
ReportDescription: this.ReportItem.rpt_description,
Application: this.ReportItem.application_name,
Owner: this.ReportItem.owner,
CubeId: this.CubeId,
ReportId: this.ReportItem.rpt_id,
ReportJson: JSON.stringify(this.getState())
}, BSModalContext));
dialog
.then((d) => d.result)
.then((r) => {
// success
console.log("Dialog ended with success: ", r);
this.RefreshReportListDropdown(r);
}, (error) => {
// failure
console.log("Dialog ended with failure: ", error);
});
进行服务调用的函数
RefreshReportListDropdown(savedReportName: any)
{
this._ClientAPIService.GetCubeReportsListByCubeWithEmptyRecord(this._SessionService.LoggedInUser, this._SessionService.SelectedApplication, this.cubeName, this._SessionService.ClientAPIServiceURL)
.subscribe(
(res) => {
this.cubeReportList = res; //This result should contain all records including the recently saved data from the popup
....
}
);
}
我的所有请求都是异步请求,即它不会等待获取返回值。 这里有什么错误?
【问题讨论】:
-
结果是订阅,不是返回值。请阅读一般的异步和 RxJS,否则你会很难。
标签: angular