I’m dealing with an http.get who sometimes returns a json (when the user is logged in) and others returns a html body, i’m adding this imports:
import {Http, RequestOptions, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
this.http.get(this.path)
.map(res => res.json()).subscribe(data => {
console.log(data})
How can i do than if i receive a html (or another kind of response) throws an error?
Wow, that’s a stupendously bad API. That being said, you can’t blindly call json()
on the response as you are doing now. Try checking the headers
property of it first, and hope that the Content-Type
is at least properly set.
Hummm… Have some code there? Im a bit lost
Hmm I woud try
this.http.get(this.path)
.map(res => res.text()).subscribe(data => {
console.log(data)
try{
json = JSON.stringify(data);
} catch (ex) {
// No json
}
}
Haven’t tried it just a guess.