[英]Error response mapping angular2
i have angular2 code for service get 我有用于服务的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);
}
if i access the url using postman. 如果我使用邮递员访问URL。 it return text like this KABUPATEN SUMENEP. 它返回像这样的KABUPATEN SUMENEP的文本。 but at my code it return Unexpected token K in JSON at position 0. How to resolve that? 但在我的代码中,它在位置0的JSON中返回了意外令牌K。如何解决该问题?
here it is my code for access the value 这是我访问值的代码
kokabname(){
this.service.getkokabname(this.gov).subscribe(
data=> this.governmentname = data,
err => err
)
}
your service should be like 您的服务应该像
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());
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.