2017-05-31 115 views
0

我無法發佈帖子。它不會觸發我的端點,並且在開發人員工具中對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://stackoverflow.com/questions/41381200/angular-2-http-get-沒有得到) – Alex

回答

4

由於可觀測量是像一個配方,他們不直到你通過調用.subscribe(「烘烤」)它們纔會被執行。 )

你的情況

您需要致電

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

啊是的。謝謝。 –