[英]How to Handle Errors in response object
在使用错误的用户名或密码进行日志记录时,它会向我发送一个错误对象作为响应
POST https://abcdserver.zxcv.com/abcdserver/connect/token 400 (Bad Request)
Error performing password flow
HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "https://abcdserver.zxcv.com/abcdserver/connect/token", ok: false, …}
如何处理这些错误。 这是我的代码
login.component.ts
jwtHelper: JwtHelper = new JwtHelper();
this.authService.loginOrg(this.userCredential.email, this.userCredential.password).then((resp) => {
let token = resp['access_token'];
if (token === null) {
this.invalidLogin = true;
}
let decodedToken = this.jwtHelper.decodeToken(token);
this.UserId = decodedToken.userid;
}, (reason) => {
this.invalidLogin = true;
});
authservice.component.ts
import { OAuthService } from 'angular-oauth2-oidc';
public loginOrg(userName: string, password: string) {
return this.oAuthService.fetchTokenUsingPasswordFlow(userName, password);
}
有两种选择:
1)实现ErrorHandler( https://angular.io/api/core/ErrorHandler )来处理应用程序中生成的所有错误,或者
2)处理你的http调用中的每个错误,如下所示:
this.getMEthod()。subscribe(resp => {your logic here},error => {your logic here})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.