my controller accepts fromBody
public async Task<IHttpActionResult> Post([FromBody]int id)
{
using (var tx = _session.BeginTransaction())
{
var p = await _session.GetAsync<Product>(id);
var c = await _session.QueryOver<Cart>().Where(x => x.ProductId == id).SingleOrDefaultAsync();
if (p != null)
{
if (c == null)
{
Cart cItem = new Cart
{
Name = p.Name,
Picture = p.Picture,
Price = p.Price,
Quantity = 1,
ProductId = p.Id,
};
await _session.SaveAsync(cItem);
}
else
{
c.Quantity = c.Quantity + 1;
await _session.SaveAsync(c);
}
}
await tx.CommitAsync();
return Ok();
}
}
in my angular side i have service which simply does post:
addTocardUrl="http://localhost:49403/api/Cart";
constructor(private http: HttpClient) { }
addToCart(x){
return this.http.post(this.addTocardUrl,{},{ params:{id:x}});
}
in my component i have :
addToCard(id){
this.service.addToCart(id).subscribe();
}
in my webconfig webserver:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
when i do the post request i get the following error : Access to XMLHttpRequest at 'http://localhost:49403/api/Cart?id=1' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.