1

I don't know what I am doing wrong, I don't get any response from the server to work using Http. I added the project as plunker if someone could have a deeper look.

https://plnkr.co/edit/3HybQY3vcsCImbuFCWrP?p=preview

post.service.ts

  getPosts(){
   return this._http.get("http://jsonplaceholder.typicode.com/posts")
                    .map(res => res.json());
  }

app.component.ts

constructor(private _postService: PostService) {
    _postService.getPosts()
               .subscribe(res => console.log(res));
  }
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33

3 Answers3

0

You are making cross-domain request

You need to add some staff to http request header:

Access-Control-Allow-Origin: *

More details: How to create cross-domain request (Angular 2)?

Community
  • 1
  • 1
Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
0

Wasn't anything wrong, worked when restarted my lite server.

Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33
0

There are two major mistakes in your code that's your code is not running as expected.

  1. you are using Http request response but you have not configure @angular/http setting in your config file so you have to add this in the file.

    '@angular/http': {
      main: 'bundles/http.umd.js',
      defaultExtension: 'js'
    },
    
  2. You are trying to get request over http which angular2 won't allow so you have to change your url like

    http://jsonplaceholder.typicode.com/posts
    

    to

    https://jsonplaceholder.typicode.com/posts
    

here is your working code demo

Working Example

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215