繁体   English   中英

Angular http 处理 json 响应在 Z80791B3AE7002CB88C246876D9FAA8F8 错误的情况下

[英]Angular http handling json response in case of http error

我无法解决这个问题并且在网上我找不到任何东西,我做了一个这样的邮寄电话:

return this.http.post(url, body, { headers: ConnectFunctions.getHeader() }).pipe(
  map(result => {
    return result;
  }),
  catchError(ConnectFunctions.handleError(url, []))
);
}

这是我从服务器得到的响应

{"status":500,"timestamp":"21-07-2020 03:51:17","message":"my custom message","details":"some details"}

我想获得变量消息(“我的自定义消息”),但我做不到。

public static handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
        console.log("Error use: " + `${operation} failed: ${error.message}`);
        console.log("Error statusText: " + error.statusText);
        return of(error as T);
    };
}

我该怎么做?

谢谢你的帮助。

有一些方法可以捕获错误,但您可以总结一下:

return this.http.post(url, body, { headers: ConnectFunctions.getHeader() })
      .catch((err: HttpErrorResponse) => {
    // simple logging, but you can do a lot more,
    console.error(err.error.message);
  });

查看更多关于 HttpErrorResponse: https://angular.io/api/common/http/HttpErrorResponse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM