1

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

1
  • You need to inject the HTTP Service Commented Jan 9, 2018 at 5:07

4 Answers 4

1

You need to add this code into your app.module.ts file.

import { NgModule, ErrorHandler } from '@angular/core';
import { HttpModule } from '@angular/http';

imports: [
   BrowserModule,
   HttpModule       
]

Actually http is used in angular 2 but now in latest version of angular it used HttpClient.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using angular v5 version, import HttpClientModule in your app.module.ts after HttpModule.

imports: [
    HttpModule,
    HttpClientModule
]

4 Comments

Using Angular 2 version not v5
what is the version
Angular version is 2
post your app.module.ts
0

you should import HttpModule in the module.ts file and the Http in the service where you are actually using it to make the api call.

Have a look here

2 Comments

There's no module.ts file.
I meant wherever you are defining you angular 2 module. may be app.module.ts
-1

you can import this line app.module.ts

    import { HttpModule } from '@angular/http';

and write in

@NgModule({
   imports: [
    HttpModule,
   ]
 })

2 Comments

Did like this. Then i'm getting this error - Uncaught Error: Unexpected value 'Http' imported by the module 'AppModule'. Please add a @NgModule annotation.
Finally it works. I have imported both Http and HttpModule. No need to import both as you have mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.