首页 文章

如何在http调用angular 2中的catch块中读取响应体

提问于
浏览
2

如何在angular 2 http请求的catch块中读取响应体 . 以下是我到目前为止的代码:

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));
                        }
                    })
    }

1 回答

  • 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');
                        }
                    })
    

相关问题