Get JSON data from API with Angular 2

Sh13

I am trying to get some JSON data by API that I have created, but it does not receive it. I have used the following Angular code:

getBook(id: string){
    return this._http.get(this.url + 'books/' + id)
               .map(res => {
                   console.log(res.json());         //It does not show anything
                   return res.json();
               })

However the getBooks() method has no problems getting the data. There are no errors in the browser console. This is the whole service code:

import { Injectable } from '@angular/core';

import { Http } from "@angular/http";
import 'rxjs/add/operator/map';
import { Observable } from "rxjs/Observable";

@Injectable()
export class LibrosService {

url: string = "http://localhost/API/public/index.php/api/";

constructor(private _http: Http) { }

getBooks(){
    return this._http.get(this.url + 'books')
                .map(res => res.json());        //it works
}

getBook(id: string){
    return this._http.get(this.url + 'books/' + id)
               .map(res => {
                   console.log(res.json());     //it does not work
                   return res.json();
               })
}

Sorry for my English if it is not very good and thank you for your help.

Sh13

Fortunately, a friend helped me find the solution because the most frustrating thing was console did not show any errors. And the problem was not in service, it was in component.

Here is my solution:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { BooksService } from "app/services/books.service";
import { Subscription } from "rxjs/Subscription";

@Component({
  selector: 'app-book',
  templateUrl: './book.component.html'
})
export class BookComponent implements OnInit {

  public book: any =[];
  private sub: Subscription;
  public errorMessage: string;

  constructor( private _activatedRoute: ActivatedRoute,
               private _booksService: BooksService ) {}

  ngOnInit() {
    this.sub = this._activatedRoute.params
                   .subscribe(params => {
                        let id = +params['id'];
                        this.getBok(id);
                    });
  }

  getBok(id){
      this._booksService.getBook(id)
          .subscribe(book => {
              this.book = book,
              error => this.errorMessage = <any>error
          });
  }
}

Thanks all of you for your help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 best practices to get data from json or api

From Dev

Get data from an API for Chartist in Angular 2

From Dev

Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

From Dev

Get Json object from url (web api) with angular 2 & Typescript

From Dev

Get data from API and iterate over in Angular2

From Dev

Get data from config.json in Angular 2

From Dev

How to get an array from JSON data in Angular 2

From Dev

How to get data from an API which requires parameters from a previous API request in Angular 2?

From Dev

Angular2 - how to get json data from server and modify json prior to mapping

From Dev

How to get JSON data from an API

From Dev

Get data from registration of user with JSON API

From Dev

Unable to get JSON data from Img2Json API for large files in PHP

From Dev

Getting data from Web API in Angular 2

From Dev

get element from json array in angular 2

From Dev

get element from json array in angular 2

From Dev

how to get data from service in angular 2?

From Dev

Get data from angular 2 pipe

From Dev

JSON data posts from Angular 2 front end to Asp.Net Core web api - loses values

From Dev

JSON data loaded from http.get() is undefined in my Angular 2 template

From Dev

Cannot get the key value from JSON Data child object in Angular2

From Dev

Can't get data from json file by GET request in Angular

From Dev

How to get values from JSON webservice with 2 objects in Angular 2

From Dev

Using a Angular controller to show JSON data from API

From Dev

Getting Json Data From API using Angular Js

From Dev

Pulling JSON data from API server in Angular 1

From Dev

Pass Json data from angular JS to spring REST API and handling it

From Dev

How to GET API data from server in Angular App?

From Dev

How to get data from rest api and show in angular js

From Dev

Can't get weather data from API using angular components

Related Related

  1. 1

    Angular 2 best practices to get data from json or api

  2. 2

    Get data from an API for Chartist in Angular 2

  3. 3

    Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

  4. 4

    Get Json object from url (web api) with angular 2 & Typescript

  5. 5

    Get data from API and iterate over in Angular2

  6. 6

    Get data from config.json in Angular 2

  7. 7

    How to get an array from JSON data in Angular 2

  8. 8

    How to get data from an API which requires parameters from a previous API request in Angular 2?

  9. 9

    Angular2 - how to get json data from server and modify json prior to mapping

  10. 10

    How to get JSON data from an API

  11. 11

    Get data from registration of user with JSON API

  12. 12

    Unable to get JSON data from Img2Json API for large files in PHP

  13. 13

    Getting data from Web API in Angular 2

  14. 14

    get element from json array in angular 2

  15. 15

    get element from json array in angular 2

  16. 16

    how to get data from service in angular 2?

  17. 17

    Get data from angular 2 pipe

  18. 18

    JSON data posts from Angular 2 front end to Asp.Net Core web api - loses values

  19. 19

    JSON data loaded from http.get() is undefined in my Angular 2 template

  20. 20

    Cannot get the key value from JSON Data child object in Angular2

  21. 21

    Can't get data from json file by GET request in Angular

  22. 22

    How to get values from JSON webservice with 2 objects in Angular 2

  23. 23

    Using a Angular controller to show JSON data from API

  24. 24

    Getting Json Data From API using Angular Js

  25. 25

    Pulling JSON data from API server in Angular 1

  26. 26

    Pass Json data from angular JS to spring REST API and handling it

  27. 27

    How to GET API data from server in Angular App?

  28. 28

    How to get data from rest api and show in angular js

  29. 29

    Can't get weather data from API using angular components

HotTag

Archive