我正在使用角5服务调用POST API
服务文件
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');
}
}我在组件中调用此服务。
import { Component, OnInit } from '@angular/core';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { IScheduleAPI } from '../interfaces/ischeduleAPI';
import { SchedulesService } from '../services/schedules.service';
import { Http, Response } from '@angular/http';
@Component({
selector: 'app-schedules',
templateUrl: './schedules.component.html',
styleUrls: ['./schedules.component.css']
})
export class SchedulesComponent implements OnInit {
Schedule: IScheduleAPI[];
message: string;
constructor(private sch: SchedulesService, private http: Http) { }
ngOnInit() {
const params = [{
'ActionMethod': 'GetSchedule',
'StaffCode': 'NA',
'Password': 'NA'
}];
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
this.sch.GetScheduleAPI(params, httpOptions).subscribe(data => this.Schedule = data,
error => this.message = error);
}
}现在的问题
1.]它显示了预期的0类型参数的错误,但是得到了1的一行
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)它的内部服务文件,这是我正在使用的标记从其他服务文件,在那里,它运行良好。
删除IScheduleAPI[]后,它停止显示错误,但不按API,而是显示错误。

我用的是角5
发布于 2018-04-17 09:04:08
您看起来好像到处都在使用新的HttpClient,但是在构造函数中。
你所需要做的就是
constructor(private http: Http) { }使用
constructor(private http: HttpClient) { }在SchedulesService和SchedulesComponent中
https://stackoverflow.com/questions/49873605
复制相似问题