0
I’m at an impasse when it comes to getting a JSON of my return that I can’t read it in any way I’ve tried in several ways I will show below how the code is now and what I need to do.
My Component method where 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 left like this, I need to get the Code and Message:
date[0] {res: Response} res : Response {_body: "{"code":0,"message":"Test"}", status: 200, ok: true, statusText: "OK", headers: Headers, ...} proto : Object
I set up 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 it to get here in 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'));
}
The same way you’re getting the status.
– Diego Souza
does not work friend, gives Undefined
– Ederson Coelho
if I try to do this gives Undefined too, this.toastr.Success("Test", data[0].res._body.message);
– Ederson Coelho