我有一个要求,我需要向 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
}
更改代码,如下所示: 映射运算符应映射数据。实际数据不是一个数组。它只是价值。
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";
});
}