Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5 Http Post不工作_Angular_Angular5 - Fatal编程技术网

Angular 5 Http Post不工作

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/

我正在使用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/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) { }