Angular 2检查状态代码200和400

时间:2017-11-17 13:09:10

标签: angular http http-status-code-404

我试图向我的服务器发送密码 - >服务器检查它是否是正确的密码 - >发送状态代码:200成功或401错误。

现在我正在检查状态代码,但感觉应该有一个更好的方法,因为我的代码下来吼一声。

我基本上只是想检查这两个状态代码。

count

1 个答案:

答案 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:'))
);