我尝试在Angular 5
应用中实施授权。我有auth.service.ts
和login()
方法,返回Observable<any>
。它看起来像这样:
login(loginForm: LoginForm): Observable<any> {
debugger;
return this.http.post(`${this.endpoint}session/login`, loginForm)
.map(res => {
return res;
}).catch(error => {
debugger;
return Observable.of(false);
});
}
问题在于,当我从组件调用login方法时,http.post
会抛出错误,它会在debugger
上停止,错误如下所示:
AppComponent.html:15 ERROR TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at HttpRequest.serializeBody (http.js:916)
at Observable.eval [as _subscribe] (http.js:2196)
at Observable._trySubscribe (Observable.js:172)
at Observable.subscribe (Observable.js:160)
at subscribeToResult (subscribeToResult.js:23)
at MergeMapSubscriber._innerSub (mergeMap.js:138)
at MergeMapSubscriber._tryNext (mergeMap.js:135)
at MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.Subscriber.next (Subscriber.js:92)
并且Chrome网络标签中没有记录XHR请求。 有什么想法吗?
答案 0 :(得分:1)
由于@JBNizet指出我试图发送一个无法转换为JSON
的对象,我检查了我的LoginForm
,它看起来像这样:
export class LoginForm {
constructor(public email?: string,
public password?: string) {}
}
我把班级改为:
export class LoginForm {
public email: string;
public password: string;
}
它现在正在工作