【发布时间】:2019-03-06 19:55:23
【问题描述】:
在我的 Angular 应用程序中,我正在使用我创建的服务中的 get 请求从 api 获取数据,我也在尝试创建对该 api 的发布请求,我的代码似乎不起作用。到目前为止,我的代码是:
import { Injectable } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class
nowService {
serviceApiUrl: string = 'api/incident';
constructor(
private http: HttpClient,
) { }
getAll(): Observable<any> {
return this.http.get<any>(this.serviceApiUrl)
.pipe(
catchError(this.handleError)
);
}
getIncidents(customerId): Observable<any> {
return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
}
postIncidents(customerId): Observable<any> {
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
}
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
console.log(error.error.message)
} else {
console.log(error.status)
}
return throwError(
console.log('Something has happened; Api is not working!'));
};
}
它将customer_id 作为参数。我得到的错误是:
预期有 2-3 个参数,但得到了 1 个。
【问题讨论】: