I am new to Angular 2 and I'm trying to POST data to Web API. For some reason my POST isn't working & I don't see the issue. The JSON.stringify
works just fine...so my issue is probably the URL or the Service.
Any help appreciated.
API CONTROLLER:
[Route("api/[controller]")]
public class SamplesController : Controller
{
#region <Actions>
// GET: api/samples/list
[HttpGet("[action]")]
public IEnumerable<SampleData> List()
{
return UnitOfWork.Samples.AsEnumerable();
}
[HttpPost("[action]")]
public void Post([FromBody]string value)
{
// Never gets here
}
#endregion
}
DATA SERVICE COMPONENT:
// MODULES
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
// MODELS
import { SampleDataModel } from "../models/sampledata.model";
@Injectable()
export class SampleDataService {
// CONSTRUCTORS
constructor(private http: Http) { }
// METHODS - public
public submit(sample: SampleDataModel) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let url: string = 'api/samples/post';
let json = JSON.stringify(sample);
return this.http.post(url, json, options)
.map(this.extractData)
.catch(this.handleError);
}
}