【发布时间】:2018-05-01 06:37:20
【问题描述】:
我正在尝试向我的服务器发送密码 -> 服务器检查密码是否正确 -> 发送状态码:200 成功或 401 错误。
现在我正在检查状态代码,但感觉应该有更好的方法,因为下面的代码感觉很笨重。
我基本上只是想检查这两个状态码。
getTokenFromServer(value: Authentication): Observable <Authentication>{
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json'})
};
return this.http.post(serverUrl, value, httpOptions).pipe(
tap((response: any) => {
if(response.status == "200"){
console.log("Success logging in: "+response.status+":"+response.token);
this.tokenService.setToken(response.token);
}else{
console.log("Error logging in: " +response.status);
}
}),
catchError(this.handleError('Authenticate password:'))
);
}
【问题讨论】:
标签: angular http http-status-code-404