I'm developing an application with angular2 as frontendI want to log in from the client side to the server side but when I try to pass the credentials I have these errors in the browser console .
In run time it complains with:
data.json is not a function
This is my service code:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class LoginService {
isAuthenticated: boolean = true;
constructor(private http: Http) {
}
login(username: string, password: string) {
const headers = new Headers();
const creds = 'username=' + username + '&password=' + password;
headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return new Promise((resolve) => {
this.http.post('http://localhost:8080/StudentManager/login', creds, { headers: headers })
.map( this.extractData )
.subscribe(
(data) => {
if(data.json().success) {
window.localStorage.setItem('auth_key', data.json().token);
console.log(username);
this.isAuthenticated = true;
}
resolve(this.isAuthenticated);
});
}
);
}
}
Here is a snapshot of the error:
