我试图向我的服务器发送密码 - >服务器检查它是否是正确的密码 - >发送状态代码:200成功或401错误。
现在我正在检查状态代码,但感觉应该有一个更好的方法,因为我的代码下来吼一声。
我基本上只是想检查这两个状态代码。
count
答案 0 :(得分:0)
只有2xx状态代码会进入您的点击功能,因此不需要if检查。 401状态码将导致引发错误,这是由catchError
函数引起的。您可以简化为:
return this.http.post(serverUrl, value, httpOptions).pipe(
tap((response: any) => {
console.log("Success logging in: "+response.status+":"+response.token);
this.tokenService.setToken(response.token);
}),
catchError(this.handleError('Authenticate password:'))
);