2017-04-26 56 views
2

如何读取角度2 http请求的catch块中的响应主体。以下是代码,我到目前为止有:如何在http call angular catch中读取catch块中的响应主体2

requeueTask(data): Observable<Response>{ 
     this.setHeaders(); 
     let url = SharedService.requeueTaskApi; 
     return this._http.put(url, data, {headers: this.headers}) 
        .map((response: Response) => response) 
        //.do(data => console.log(JSON.stringify(data))) 
        .catch((error: any, response) => { 
         console.log(response); 
         if (error.status < 400 || error.status ===500) { 
          return Observable.throw(new Error(error.status)); 
         } 
        }) 
    } 
+0

使用error.json() –

回答

4

你能赶上的错误这样如下:

return this._http.put(url, data, {headers: this.headers}) 
       .map((response: Response) => response) 
       .catch((error: Response) => { 
        console.log(response); 
        if (error.status < 400 || error.status ===500) { 
         return Observable.throw(error.json().error || 'Server error'); 
        } 
       }) 
+0

嗨,我的错误响应已经在JSON和我不没有.error属性,错误信息在_body中...我已经返回了这个?例如:return Observable.throw(error._body ||'Server error'); – Hazlo8