Search code examples
angularangular4-forms

Function throws error [ts] Property 'json' does not exist on type 'Object' in Angular4


I am working in an Angular4 application in this I need to consume the API response .For that I have put the below function and it throws the error as in my title.

I have referred some Stackoverflow posts but nothing is worked for me .

ngOnInit(){
 this.CartdataService.get_Product_Categories().subscribe(
      data => {
        this.dropdownData = data.json();
      });
}

Here in this line I got the mentioned error

this.dropdownData = data.json();

These are my Headers

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { CartdataService } from '../../services/cartdata.service';
import { HttpClient } from '@angular/common/http';

Solution

  • import { HttpClient } from '@angular/common/http';

    In the new HttpClient, you don't have to parse your result to JSON.

    Simply remove the line, and it should work.