I'm developing a Angular 2 application to access REST API endpoints and show the data to users. I tested REST API endpoints in Postman and those are working well. When i'm accessing a GET endpoint from Angular i'm getting following error snippet.
Error: StaticInjectorError[Http]:
StaticInjectorError[Http]:
NullInjectorError: No provider for Http!
at _NullInjector.get (webpack-internal:///../../../core/esm5/core.js:1189)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveNgModuleDep (webpack-internal:///../../../core/esm5/core.js:11074)
at NgModuleRef_.get (webpack-internal:///../../../core/esm5/core.js:12306)
at resolveDep (webpack-internal:///../../../core/esm5/core.js:12804)
Tried by importing both Http and HttpModule like this top of the code as well.
import { HttpModule } from '@angular/http';
import {Http} from '@angular/http';
Following is my relevant code snippet for the get data function.
getData(){
const url = 'http://localhost/laravel_app/public/api/getdata';
this.http.get(url).subscribe(
res => {
const data = res.json();
console.log(data);
return data;
}
);
}