Angular 5 http帖子没有发送请求

时间:2018-02-24 10:35:36

标签: angular typescript http angular-services angular-httpclient

我尝试在Angular 5应用中实施授权。我有auth.service.tslogin()方法,返回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请求。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

由于@JBNizet指出我试图发送一个无法转换为JSON的对象,我检查了我的LoginForm,它看起来像这样:

export class LoginForm {
    constructor(public email?: string,
        public password?: string) {}
}

我把班级改为:

export class LoginForm {
    public email: string;
    public password: string;
}

它现在正在工作