如何在Angular 8中获取HTTP错误响应文本?

huangapple go评论68阅读模式
英文:

How to get http error response text in angular 8?

问题

这是要翻译的代码部分:

this.authservice.login.subscribe(res => {
    this.router.navigateByUrl('/');
  }, err => {
    this.error = err; // this err returns "Bad request" instead of backend's response text!?
  }));

请注意,我只翻译了代码部分,不包括问题或其他内容。

英文:
this.authservice.login.subscribe(res => {
    this.router.navigateByUrl('/');
  }, err => {
    this.error = err; // this err returns "Bad request" instead of backend's response text!? 
  }));

Can anyone help me? here in err part I didn't get response text from back-end it shows only "Bad Request" every time.

答案1

得分: 0

添加了错误处理程序。

英文:
import { HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';

export class ErrorHandler {
    public static handleError(error: HttpErrorResponse) {
        return throwError(error.error);
    }
}

added error handler.

huangapple
  • 本文由 发表于 2020年1月3日 19:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定