Wouter Vandenputte
Wouter Vandenputte

Reputation: 2103

Angular 2 import Http failed

Dear Stackoverflow community,

I'm tyring to do a http-request to get some JSON data.

import {Injectable} from "angular2/core";
import { Http }  from '@angular/http';
import {Trainer} from "./trainer"

@Injectable()
export class TrainerService
{
    private baseUrl : string = 'http://10.161.48.16:8080/campus/trainers';

    constructor(private http : Http)
    {}

   getAllTrainers()
   {
       let trainer$ = this.http
      .get(this.baseUrl);
      return trainer$;
   }
}

But the console logs this:

17.03.20 13:17:00 404 GET /@angular/http

After lots of interweb searching, it seems to be a problem with the import of the http module, (altough my autocomplete can find all methods on it). I could also be wrong.

I'm a complete newbie to this and I really cannot find the solution.

package.json

 {
  "name": "campus-webapp",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/http": "~2.4.0",
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings":"^0.6.8"
  }
}

typings.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "baseUrl": "map",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/Rx.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js"></script>
    <script>
      System.config({
        transpiler: 'typescript',
        typescriptOptions: { emitDecoratorMetadata: true },
        packages: {'app': {defaultExtension: 'ts'}},
        map: { 'app': './app' }
      });
      System.import('app/service_main')
            .then(null, console.error.bind(console));
    </script>
  </head>
<body>
   <my-app>Loading...</my-app>
</body>
</html>

And yes my code is in app/ and not src/app

Upvotes: 1

Views: 215

Answers (2)

eko
eko

Reputation: 40647

You have inconsistency in your imports.

import {Injectable} from "angular2/core";

is beta version import whereas

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

is rc+

If you are using beta version of angular2 (Since you are importing https://code.angularjs.org/2.0.0-beta.6/angular2.dev.js in your index.html) make sure you import the Http from the correct package (angular2/http).

But I suggest you to upgrade your app to the latest version and follow the latest guide on the official site.

Changelog: https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02

Upvotes: 3

Vikram Pawar
Vikram Pawar

Reputation: 138

import {Injectable} from "angular2/core";//Beta version

insted use this

import {Injectable} from "@angular/core";//final version

Upvotes: 1

Related Questions