我不能发帖子。它不会触发我的端点,并且开发人员工具中没有针对web api的请求。
Http服务:
@Injectable() 导出类DataService {
private _apiurl: string;
private _headers: Headers;
private _options: RequestOptions;
private _apiEndpoint : string;
constructor(public http: Http, public _securityService : OidcSecurityService) {
this.setHeaders();
}
public setEndpoint(endpointUrl : string){
this._apiurl = AppSettings.API_URL + endpointUrl;
}
private setHeaders() {
console.log('setHeaders started');
this._headers = new Headers();
this._headers.append('Content-Type', 'application/json');
this._headers.append('Accept', 'application/json');
let token = this._securityService.GetToken();
if (token !== '') {
let tokenValue = 'Bearer ' + token;
console.log('tokenValue:' + tokenValue);
this._headers.append('Authorization', tokenValue);
}
this._options = new RequestOptions({ headers: this._headers, body: '' });
}
public GetAll = (): Observable<any[]> => {
return this.http.get(this._apiurl, this._options).map((response: Response) => <any[]>response.json());
}
public GetSingle = (id: number): Observable<any> => {
return this.http.get(this._apiurl + '/' + id, this._options).map(res => <any>res.json());
}
public Create = (item: any): Observable<any> => {
console.log(JSON.stringify(item) + " keke")
return this.http.post(this._apiurl, JSON.stringify(item), this._options).map(res => <any>res.json());
}
public Update = (id: number, item: any): Observable<any> => {
return this.http.put(this._apiurl + '/' + id, JSON.stringify(item), this._options).map(res => <any>res.json());
}
public Delete = (id: number): Observable<any> => {
return this.http.delete(this._apiurl + id);
}
}
组件:
create() {
var result,
userValue = this.form.value;
console.log(userValue.Id + ", userValueId");
this.dataService.setEndpoint('/api/program/create');
this.dataService.Create(userValue);
}
答案 0 :(得分:4)
因为observable就像一个食谱,直到你通过调用来“烘烤”它们才会被执行.subscribe()
在您的情况下,您需要致电
this.dataService.Create(userValue).subscribe();