Angular2不发布帖子

问题描述:

我无法发布帖子。它不会触发我的端点,并且在开发人员工具中对web api没有要求。Angular2不发布帖子

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

的[角2 HTTP GET得不到]可能的复制(https://*.com/questions/41381200/angular-2-http-get-没有得到) – Alex

由于可观测量是像一个配方,他们不直到你通过调用.subscribe(“烘烤”)它们才会被执行。 )

你的情况

您需要致电

this.dataService.Create(userValue).subscribe(); 
+0

啊是的。谢谢。 –