【问题标题】:How to get return values from http post method in Angular2?如何从Angular2中的http post方法获取返回值?
【发布时间】: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
            ....
        }
    );
}

我的所有请求都是异步请求,即它不会等待获取返回值。 这里有什么错误?

【问题讨论】:

标签: angular


【解决方案1】:

在SaveData方法中返回http.post调用的结果。

SaveData(userName: any, cubeReport: CubeReportViewModel, APIServiceURL: any)
{
    return this._http.post(APIServiceURL, JSON.stringify(cubeReport), { headers: ContentHeaders })
        .map((res: Response) => res.json())
        .do(() => {
            console.log('request finished');
        });
}

在此之后,您的消费者组件中的 subscribe 将正常工作。

【讨论】:

  • 仍然无法正常工作或按预期工作。我在关闭 angular2-modal 弹出窗口时调用上述服务。保存记录后,模式弹出窗口关闭,我需要创建一个新服务来获取记录列表,包括最近保存的数据,并在调用弹出窗口的父组件中绑定一个下拉列表。但是目前它没有在列表中获取最近保存的记录? (请参考编辑后的问题)
  • 您是否有任何示例,例如 Plunker 中的示例,以便我可以看到问题的实际效果?
  • 现在工作正常,是我的错误。早些时候我调用了 SaveCubeReport 方法,它就像 angular2 组件和 api 服务调用之间的一个额外包装器。但是当我删除它并直接从我的 angular2 组件中调用 SaveData() 方法时,它工作正常。
【解决方案2】:

登录服务.ts:

  checklogin(user) {   
   return this.http.post("API_URL",user).map(resp => resp.json());
  }

登录组件.ts:

   localvariable={} // or whatever u want 

   customFunction () {
     this.loginservice.checklogin(this.user).subscribe (data => 
     {this.localvariable=data;}) 

     // results is assigned now to this.localvariable
     //write your logic etc ... 
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多