我正在尝试让我的身份验证服务和登录组件发挥得很好。但它似乎没有奏效。基本上,登录时我的服务器返回{user:user}或{error:errorMessage}。
由于显而易见的原因,我不想在我的组件中解析响应,我需要它来返回用户或错误字符串。成功,似乎工作得很好,我登录,用户返回登录组件,但出于某种原因,出错时,整个Response对象返回到登录组件。然后我需要解析。告诉我我做错了什么。谢谢 !
AuthService类中的登录功能
login(UserLogin): Observable<any> {
let params: URLSearchParams = new URLSearchParams();
params.set('action', 'login');
params.set('username', UserLogin.username);
params.set('password', UserLogin.password);
return this.http.get(this.apiUrl, {search: params, withCredentials: true})
.map(
response => response.json().user as User,
response => Observable.throw(response.json().error)
)
}
这是LoginComponent类中的登录功能
onSubmit() {
this.authService.login(this.user)
.subscribe(response => {
console.log('login success', response);
this.activeModal.close();
this.router.navigate(['/dashboard']);
}, response => {
console.log('login error', response);
this.errorMessage = response.json().error; <= HERE!!!
});
}
我需要再次解析响应以获取错误。这似乎不对。我觉得我错过了什么。