Why is not working in Angular map 4?

0 like 0 dislike
12 views
Good day!
I can't rode, why the content of the response.json() is empty in Angular4 (Angular CLI). Prompt where a jamb?

Code the following:
import { Injectable } from '@angular/core'; import { Response } from '@angular/http'; import { Observable } from 'rxjs'; import 'rxjs/add/operator/map' import {HttpClient, HttpHeaders} from "@angular/common/http"; @Injectable() export class AuthenticationService { public token: string; constructor(private to http: HttpClient) { var currentUser = JSON.parse(sessionStorage.getItem('currentUser')); this.token = currentUser && currentUser.token; } login(username: string, password: string): Observable { return this.http.post('http://apiserver.com/api/v1/login', JSON.stringify({ username: username, password: password }) .map((response: Response) => { var token = response.json() && response.json().token; if (token) { // set the token property this.token = token; localStorage.setItem('currentUser', JSON.stringify({username: username, currentUser: token})); return true; } else { return false; } } }
by | 12 views

2 Answers

0 like 0 dislike
For HttpClient does not need to use json()
var token = response && response.token
by
0 like 0 dislike
HttpClient works is that the map will arrive ready rasparenny json response, not the Response.
by

Related questions

110,608 questions
257,186 answers
0 comments
33,867 users