Tanwer
Tanwer

Reputation: 1583

Angular 5 Http Post Not working

I am invoking a POST API using Angular 5 Service

Service file

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');
  }
}

I am calling this service in my Component

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);
  }
}

Now Problems

1.] It show error that Expected 0 type arguments but got 1 in line

return this.http.post<IScheduleAPI[]>(this.apiURL, params, httpOptions)

Its inside service file , this is the markup I am using from other service file where it is working fine .

After Removing IScheduleAPI[] it stops showing error but does not hit API than , instead show error

enter image description here

I am using Angular 5

Upvotes: 1

Views: 1207

Answers (1)

David
David

Reputation: 34425

You look like you are using the new HttpClient everywhere, but in the constructor.

All you need to do is to replace

constructor(private http: Http) { }

with

constructor(private http: HttpClient) { }

in both SchedulesService and SchedulesComponent

Upvotes: 1

Related Questions