简体   繁体   English

如何从Angular2的http post方法获取返回值?

[英]How to get return values from http post method in Angular2?

I have a requirement where I need to make a http post method request to a Web API ie save some data entered by the user. 我有一个要求,我需要向Web API发出http post方法请求,即保存用户输入的一些数据。 The Web API would return some values as a result and this result would be used for some UI logic. Web API将作为结果返回一些值,并且该结果将用于某些UI逻辑。

I tried making the http post in synchronous way, but it doesn't work properly as expected. 我尝试以同步方式制作http帖子,但无法按预期正常工作。

Angular2 component calls this service to save the data:- 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 method call is as follows:- 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;
}

Parent component where the angular2-modal popup is invoked. 调用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);
});

function from where the service call is made 进行服务呼叫的地方的功能

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

All my request are made a asynch requests ie it doesn't wait to get the return values. 我所有的请求都是发出一个异步请求,即它不等待获取返回值。 What is the mistake here? 这是什么错误?

Return the result of http.post call in the SaveData method. 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');
        });
}

After this your subscribe in your consumer component will work correctly. 此后,您在消费者组件中的subscribe将正常工作。

loginservice.ts : loginservice.ts:

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

logincomponent.ts : logincomponent.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 ... 
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM