Angular 5 Http Post不工作
我正在使用Angular 5服务调用PostAPI 服务文件Angular 5 Http Post不工作,angular,angular5,Angular,Angular5,我正在使用Angular 5服务调用PostAPI 服务文件 import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpErrorResponse, HttpClientModule } from '@angular/common/http'; import { Http, Response } from '@angular/http'; import 'rxjs/add/operator/
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse, HttpClientModule } from '@angular/common/http';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { HttpResponse } from 'selenium-webdriver/http';
import { IScheduleAPI } from '../interfaces/ischeduleAPI';
// import { Ischedule } from '../interfaces/ischedule';
@Injectable()
export class SchedulesService {
apiURL = 'http://localhost:57863/api/Audit/';
ScheduleDetail: IScheduleAPI[] = [];
constructor(private http: Http) { }
GetScheduleAPI(params, httpOptions) {
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)
.catch(this.errorHandler);
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || 'Server Error');
}
}
现在的问题
1.]它显示了一个错误,该错误应为0个类型参数,但行中有1个
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)
返回this.http.post(this.apirl、params、httpOptions)
它的内部服务文件,这是我在其他服务文件中使用的标记,它工作正常
删除isScheduleAPI[]后,它将停止显示错误,但不会命中API,而是显示错误
我正在使用Angular 5您看起来像是在到处使用新的
HttpClient
,但在构造函数中
你所需要做的就是更换
constructor(private http: Http) { }
与
在
SchedulesService
和SchedulesComponent
中,您混合了旧的Http和新的HttpClient模块Http
被弃用使用HttpClient
而不是将构造函数(私有Http:Http){}
替换为构造函数(私有Http:HttpClient){}
。看来你已经拿到进口货了right@David,它使用HttpClient编译并命中api,但它不会等待api返回结果,而是在结果之前执行其他语句return@Jack:返回前执行的代码是什么?您知道http请求是异步的,对吗?
constructor(private http: Http) { }
constructor(private http: HttpClient) { }