如何从 API 调用/获取 out 参数到 Angular4



在这里,我正在使用角度 4 Web 应用程序的 post 方法。输入数据并单击确认按钮时,值(数据(将保存到数据库中。它工作成功,但我的问题是在 post 方法完成的同时,另一个函数参数的 ID(返回值(正在从数据库传递到 API(对于 Angular(。在这里,如何将该值带到我的角度?我希望该值作为下一个函数参数。实际上我是角度新手,如果我的问题有任何问题,请原谅我。 (我希望 TransactionId 值从 api 变为角度(

这是我的打字稿(TS文件(

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";
});
additem{
this.items = new IStockitems(this.shopid,value.ItemCode, value.ItemDescription, value.PackingtypeName, value.Stock, this.TransactionId );
}   //here this.TransactionId is the returned value from the above post method.

这是我的服务文件

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

这是我的 WebAPI(注意我的 API 成功返回值(

My API is(it is working fine)

public class StockcountheaderController : ApiController
{
private adminv2Entities enqentities = new adminv2Entities();
[HttpPost]
private  IActionResult 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);
}
return Ok(new { data = TransactionId.Value});    
}

在 API 代码中,您将返回看起来像包含整数值的 json 对象。在服务中,您已指定响应将是类型为IHeaderStock的值数组。

因此,您可以在任何一侧进行一些更改来解决此问题。首先,您可以从服务中删除IHeaderStock<>类型,如下所示

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

}

问题是在你的 API 方法中你返回return "Ok(new { data = TransactionId.Value});"但在服务方法或函数中你返回

IHeaderstock ARRAY .map((response: Response) => <IHeaderstock[]>response.json()),您应该在服务中以角度返回整数类型,或者将事务值更改为 IHeaderstock 数组。

相关内容

  • 没有找到相关文章

最新更新