Ich bin neu in Angular 2 und ich versuche, Daten an Web API POST. Aus irgendeinem Grund funktioniert mein POST nicht & Ich sehe das Problem nicht. Die JSON.stringify
funktioniert gut ... so ist mein Problem wahrscheinlich die URL oder der Service.Veröffentlichen von Daten in Angular2
Jede Hilfe wird geschätzt.
API REGLER:
[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);
}
}
Abonnieren Sie es woanders? – Dylan
Nicht sicher, was Sie unter ABONNIEREN verstehen. Aber ... this.http.post ... tut nichts. Keine Fehler .... nein nichts. Ich traf Break-Punkte in der Funktion –
Die beobachtbare http benötigt die Subskriptionsgruppe (normalerweise in Ihrer Komponente) wie - 'yourService.submit(). Subscribe ((Daten) => {...})'. – Dylan