Angular 错误响应映射角度2
我有angular2代码用于获取服务Angular 错误响应映射角度2,angular,Angular,我有angular2代码用于获取服务 getkokabname(id){ let url = "http://localhost:8080/regencies/name/"+id; let header = new Headers({'Content-Type': 'text/plain;charset=UTF-8'}); return this.http.get(url, {headers: header}) .map(this.extractData) .catch(th
getkokabname(id){
let url = "http://localhost:8080/regencies/name/"+id;
let header = new Headers({'Content-Type': 'text/plain;charset=UTF-8'});
return this.http.get(url, {headers: header})
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res:Response) {
let body = res.json();
return body || [];
}
private handleError(error:any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
如果我使用邮递员访问url。它返回的文本类似于KABUPATEN SUMENEP。但在我的代码中,它在JSON中的位置0处返回意外的标记K。如何解决这个问题
这是我访问值的代码
kokabname(){
this.service.getkokabname(this.gov).subscribe(
data=> this.governmentname = data,
err => err
)
}
你的服务应该是
getkokabname(id){
let url = "http://localhost:8080/regencies/name/"+id;
let header = new Headers({'Content-Type': 'text/plain;charset=UTF-8'});
return this.http.get(url, {headers: header})
.map(this.extractData)
.catch(
(error: Response) => {
return Observable.throw(error.json());
});
}