I have a deadlock in getting a JSON of my return that I can not read it in any way I have already tried in several ways I will show below how the code is now and what I need to do.
My Component method where it is returning what I want.
onSubmit(loginUsuario, senhaUsuario) {
this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
data => {
if (data[0].res.status == 200) {
this.usuario : Usuario = new Usuario(data[0].res._body.code, data[0].res._body.message)
this.toastr.success("Teste", this.usuario.message);
} else if (data[0].res.status == 500) {
this.toastr.error("Teste", data.toString());
}
//data => this.data = data,
//err => this.erro = <any>err
},
error => {
console.log(error);
}
);
}
}
In my console typing date [0], I need to get Code and Message:
date [0] {res: Response} res : Response {_body: "{" code ": 0," message ":" Test "}, status: 200, ok: true, statusText:" OK " proto : Object
I set my Service like this:
login(loginUsuario: string, senhaUsuario: string): Observable<Response> {
return this.http.get(this.apiUrl + '/login/' + loginUsuario + '/senha/' + senhaUsuario)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
It worked but I need to get here in the component the status:
onSubmit(loginUsuario, senhaUsuario) {
this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
data => {
//if (data[0].res.status == 200) {
//this.usuario : Usuario = new Usuario(data[0].res._body.code, data[0].res._body.message)
this.toastr.success("Teste", data.message);
//} else if (data[0].res.status == 500) {
//this.toastr.error("Teste", data.toString());
//}
//data => this.data = data,
//err => this.erro = <any>err
},
error => {
console.log(error);
}
);
}
Solution:
My Component:
onSubmit(loginUsuario, senhaUsuario) {
this.usuarioService.login(loginUsuario, senhaUsuario).subscribe(
data => {
if (data.status == 200) {
this.data = data.json();
this.toastr.success("Teste", this.data.message);
}
},
error => {
console.log(error);
}
);
}
My Service:
login(loginUsuario: string, senhaUsuario: string): Observable<Response> {
return this.http.get(this.apiUrl + '/login/' + loginUsuario + '/senha/' + senhaUsuario)
.map((res:Response) => res)
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}