我有服务获取的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(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);
}
如果我使用邮递员访问网址。它返回像这样的KABUPATEN SUMENEP文本。但在我的代码中,它在位置0的JSON中返回意外的标记K.如何解决?
这是我访问值
的代码kokabname(){
this.service.getkokabname(this.gov).subscribe(
data=> this.governmentname = data,
err => err
)
}
答案 0 :(得分:1)
您的服务应该像
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());
});
}