Angular,从 post 请求中获取状态码值
Angular, get status code value from post request
我想从 post 请求中获取 StatusCode 值,以便在我的组件中使用它。
这就是我所做的:
Api 通话:
Login(user: User) {
return this.http.post(apiUrl + 'account/Login', user).subscribe();
}
组件中的方法:
Login() {
this.user.UserName = this.loginForm.controls.userName.value;
this.user.Password = this.loginForm.controls.password.value;
this.api.Login(this.user)
}
现在只显示为错误
结果应该是这样的:
更新
这不是 cors 问题...
登录成功:
在您的订阅中添加错误回调以捕获来自 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
})
}
如果你想获取所有的状态码,不管成功还是失败,你必须在你的请求中观察response
。
将您的 API 电话设为:
this.http.post(apiUrl + 'account/Login', user, {observe: 'response'})
记录响应以查看如何访问状态。
我想从 post 请求中获取 StatusCode 值,以便在我的组件中使用它。
这就是我所做的:
Api 通话:
Login(user: User) {
return this.http.post(apiUrl + 'account/Login', user).subscribe();
}
组件中的方法:
Login() {
this.user.UserName = this.loginForm.controls.userName.value;
this.user.Password = this.loginForm.controls.password.value;
this.api.Login(this.user)
}
现在只显示为错误
更新
这不是 cors 问题...
登录成功:
在您的订阅中添加错误回调以捕获来自 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
})
}
如果你想获取所有的状态码,不管成功还是失败,你必须在你的请求中观察response
。
将您的 API 电话设为:
this.http.post(apiUrl + 'account/Login', user, {observe: 'response'})
记录响应以查看如何访问状态。