I need to send a http post request to a Web API in order to save user-entered data. The Web API will return some values, such as the TransactionId, which will be used for further logic in other functions. I'm new to Angular and although I've seen similar questions, I'm still struggling with it. It's a challenging task for me, so any help would be greatly appreciated.
Here is the Http post method call:
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);
}
The Angular4 component invokes this service to save the data:
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";
});
This is how MY API looks like:
[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
}