0

I'm using Angular 2.2 and running into problems with what should be a very simple HTTP request using @angular/http

EDIT:

Error: No provider for ConnectionBackend

I thought solved this error by importing ConnectionBackend from @angular/http and adding as a Provider in app.module.ts.
But on stop and start of the server I get this error.

Type 'typeof ConnectionBackend' is not assignable to type 'Provider'

So I removed ConnectionBackend from import and Provider.

I have tried the following to no avail:

Angular2/http Exception no ConnectionBackend

No provider for ConnectionBackend

Angular 2 : No provider for ConnectionBackend

app.module.ts:

import { NgModule }           from '@angular/core';
import { BrowserModule }      from '@angular/platform-browser';
import { HttpModule }         from '@angular/http';

import { AppComponent }       from './app.component';
import { CustomService }    from './custom.service';

@NgModule({
  imports: [ 
    BrowserModule,
    HttpModule
  ],
  declarations: [ 
    AppComponent 
  ],
  providers: [
    CustomService
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

custom.service.ts:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/Rx';

@Injectable()
export class CustomService{

   constructor( private _http: Http){}

   getJson() {
      return this._http.get('app/file.json')
        .toPromise()
        .then(response => response.json())
   }
}

app/file.json:

[
   {"id": 1, "name": "test1"},
   {"id": 2, "name": "test2"},
   {"id": 3, "name": "test3"},
   {"id": 4, "name": "test4"},
   {"id": 5, "name": "test5"}
]

package.json angular dependencies:

  "dependencies": {
    "@angular/common": "~2.2.0",
    "@angular/compiler": "~2.2.0",
    "@angular/core": "~2.2.0",
    "@angular/forms": "~2.2.0",
    "@angular/http": "~2.2.0",
    "@angular/platform-browser": "~2.2.0",
    "@angular/platform-browser-dynamic": "~2.2.0",
    "@angular/router": "~3.2.0",
    "@angular/upgrade": "~2.2.0",

I can't be the only person experiencing this issue. I've started over making a very basic application, but I get the same error.

Community
  • 1
  • 1
ppovoski
  • 4,553
  • 5
  • 22
  • 28
  • Have you followed the tutorial on HTTP Client from the Angular website (https://angular.io/docs/ts/latest/guide/server-communication.html)? It mentions that you need to import `HttpModule` in your `NgModule`. – Sjoerd Nov 23 '16 at 17:34
  • I tried that, with no luck. I will add that to the question. – ppovoski Nov 23 '16 at 17:36
  • I don't see any reason to use ConnectionBackend. – Manish Nov 23 '16 at 17:53
  • Could you have look at the following Plunker and see what is different in your setup? If you open the console you will see that everything is working (data is retrieved and no errors). http://plnkr.co/edit/M1O01vllLbvTLnJsKKTe?p=preview – Sjoerd Nov 23 '16 at 17:54
  • @user32, the only reason is to get rid of the error in the console. – ppovoski Nov 23 '16 at 17:55
  • @Sjoerd, which version of angular is plnkr using? – ppovoski Nov 23 '16 at 17:57
  • It is just some modified code coming from the Angular.io website, so it is the latest version (2.2). – Sjoerd Nov 23 '16 at 18:00
  • @Sjoerd, I used files from your plunkr to replace some of my files, and the app works... mostly. There are differences in the systemjs.config.js and differences in versions of libraries being loaded by your index.html. I have to look closer. It should not be this difficult to get http working. I wrote two other apps on an older version of Angular2 and used a slightly modified version of my service and it worked fine. Thanks for the help. If I find the exact issue, I'll post an answer. – ppovoski Nov 23 '16 at 22:16

0 Answers0