【问题标题】:Get return values from http post method in Angular4 is not working Fine从Angular4中的http post方法获取返回值无法正常工作
【发布时间】:2020-01-06 23:40:56
【问题描述】:

我有一个要求,我需要向 Web API 发出 http post 方法请求,以保存用户输入的一些数据。 Web API 将返回一些值(TransationId)作为结果,这个结果将用于其他一些逻辑(对于其他函数参数等......)实际上我看到了几个这样的问题,但它们对我没有帮助。因为我是新的角度。 请帮我解决这个问题,这对我来说是一项艰巨的任务

http post方法调用如下:-

    CatchHeaderDetail(stock: any)
    : Observable<IHeaderstock[]> {
    let stockheaderdetail = stock;
    console.log(stockheaderdetail)
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this._http.post('http://localhost:3041/api/Stockcountheader/' + 'Stock', stockheaderdetail, options)
        .map((response: Response) => <IHeaderstock[]>response.json())
        .catch(this.handleError);

}

Angular4 组件调用这个服务来保存数据:

Onclick(){

        this.header = new IHeaderstock(this.userid, this.created, this.CompanyID, this.modified, this.modifieduserid, this.confirm, this.shopid);
    this.headeritems.push(this.header);


    this._enqService.CatchHeaderDetail(this.headeritems)
        .subscribe(value => {
            if (typeof value !== 'undefined' && value != null) {
                value.forEach(header => {
                    this.headeritems.push(this.header)
                });

            }
        },

            error => {
                console.error(error);
                this.statusMessage = "Problem with the service.Please try again after sometime";
            });

我的 API 是

        [HttpPost]
    public IHttpActionResult Stock([FromBody] List<spGetNewStockCountHeader_Result> jsonvalues)
    {

        ObjectParameter TransactionId = new ObjectParameter("TransactionId", typeof(Int32));
        foreach (spGetNewStockCountHeader_Result Datastock in jsonvalues)
        {

            spGetNewStockCountHeader_Result Stockobject = new
            spGetNewStockCountHeader_Result();
            Stockobject.UserID = Datastock.UserID;
            Stockobject.created = Datastock.created;
            Stockobject.CompanyID = Datastock.CompanyID;
            Stockobject.modified = Datastock.modified;
            Stockobject.modifieduserid = Datastock.modifieduserid;
            Stockobject.confirm = Datastock.confirm;
            Stockobject.ShopId = Datastock.ShopId;

            enqentities.spGetNewStockCountHeader(Datastock.UserID,  Datastock.created, Datastock.CompanyID, Datastock.modified,
                Datastock.modifieduserid, Datastock.confirm,
            Datastock.ShopId, TransactionId).First();

        }

        return Ok(new { data = TransactionId.Value }); // i want this value to get in the angular4
    }

【问题讨论】:

  • 您遇到的错误是什么?
  • @Sudhakar errors.ts:42 错误类型错误:value.forEach 不是函数
  • @Sudhakar 当我使用邮递员检查 API 时,我得到了值。
  • 你能显示示例响应吗?
  • @Sudhakar { "数据": 123 }

标签: angular typescript angular-services angular4-forms


【解决方案1】:

更改代码如下所示:map 运算符应该映射数据。实际数据不是数组。这只是价值。

CatchHeaderDetail(stock: any): Observable<any> {
        let stockheaderdetail = stock;
        console.log(stockheaderdetail)
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        return this._http.post('http://localhost:3041/api/Stockcountheader/' + 'Stock', stockheaderdetail, options)
            .map((response: Response) => {
            return response.data;
            })
            .catch(this.handleError);

    }


Onclick(){
        this.header = new IHeaderstock(this.userid, this.created, this.CompanyID, this.modified, this.modifieduserid, this.confirm, this.shopid);
    this.headeritems.push(this.header);


    this._enqService.CatchHeaderDetail(this.headeritems)
        .subscribe(value => {
        if (value) {
            //use the value  for further process. Here value is integer value not an array.
        }
    },error => {
                console.error(error);
                this.statusMessage = "Problem with the service.Please try again after sometime";
            });

}

【讨论】:

  • .....if (value) { //将值用于进一步处理是什么意思。这里的值是整数值而不是数组。 } }, ???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
相关资源
最近更新 更多