Sto*_*orm 3 http-status-code-401 angular angular7
我想从发布请求中获取 StatusCode 值,以便在我的组件中使用它。这就是我所做的:
Api 调用:
Login(user: User) {
return this.http.post(apiUrl + 'account/Login', user).subscribe();
}
Run Code Online (Sandbox Code Playgroud)
组件中的方法:
Login() {
this.user.UserName = this.loginForm.controls.userName.value;
this.user.Password = this.loginForm.controls.password.value;
this.api.Login(this.user)
}
Run Code Online (Sandbox Code Playgroud)
在订阅中添加错误回调以捕获来自 HTTP Observable 的错误。
Login(user: User) {
return this.http.post(apiUrl + 'account/Login', user).subscribe(
(data) => {
},
(error) => {
console.log(error);
// get the status as error.status
})
}
Run Code Online (Sandbox Code Playgroud)
如果您想获得所有状态代码,无论成功还是失败,您都必须response在请求中观察。
让您的 API 调用如下:
this.http.post(apiUrl + 'account/Login', user, {observe: 'response'})
Run Code Online (Sandbox Code Playgroud)
记录响应以查看如何访问状态。
| 归档时间: |
|
| 查看次数: |
10842 次 |
| 最近记录: |