首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角5 Http Post不工作

角5 Http Post不工作
EN

Stack Overflow用户
提问于 2018-04-17 08:45:46
回答 1查看 1.2K关注 0票数 1

我正在使用角5服务调用POST API

服务文件

代码语言:javascript
复制
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');
  }
}

我在组件中调用此服务。

代码语言:javascript
复制
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的一行

代码语言:javascript
复制
return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)

它的内部服务文件,这是我正在使用的标记从其他服务文件,在那里,它运行良好。

删除IScheduleAPI[]后,它停止显示错误,但不按API,而是显示错误。

我用的是角5

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-17 09:04:08

您看起来好像到处都在使用新的HttpClient,但是在构造函数中。

你所需要做的就是

代码语言:javascript
复制
constructor(private http: Http) { }

使用

代码语言:javascript
复制
constructor(private http: HttpClient) { }

SchedulesServiceSchedulesComponent

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49873605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档