【问题标题】:status 0 from the server in http post methodhttp post方法中来自服务器的状态0
【发布时间】:2021-03-17 19:36:58
【问题描述】:

我正在尝试使用可以响应 access_token 和 refresh_token 的 API 在登录阶段测试我的项目,因此我使用 Postman 平台测试了该 API,然后获得了成功:

然后在我的 Angular 项目中使用以下语句进行测试:

 userLogin(userPayload): Observable<boolean> {
        return this.http.post("http://localhost:3000/auth/login", userPayload)
            .pipe(
                map((value: any) => { 
                    if (value) {
                        localStorage.setItem("access_token", value.access_token);
                        localStorage.setItem("refresh_token", value.refresh_token);
                        const decryptedUser = this.jwtHelper.decodeToken(value.access_token);
                        console.log(decryptedUser);
                        const data = {
                            access_token: value.access_token,
                            refresh_token: value.refresh_token,
                            username: decryptedUser.username,
                            userid: decryptedUser.sub,
                            tokenExpiration: decryptedUser.user
                        };
                        this.userInfo.next(data);
                        return true;
                    }
                    return false;
                    
                })
            );
    }

这是my API

当我为我的项目提供服务时,我收到了这个错误,我不知道为什么请求不正确:

【问题讨论】:

  • 你是否从角度侧传递了 api 请求中的标头?

标签: angular api server nest


【解决方案1】:

你能试试下面的代码吗?

userLogin(userPayload): Observable<boolean> {
        return this.http.post("http://localhost:3000/auth/login", userPayload, {headers : new HttpHeaders({ 'Content-Type': 'application/json' })})
            .pipe(
                map((value: any) => { 
                    if (value) {
                        localStorage.setItem("access_token", value.access_token);
                        localStorage.setItem("refresh_token", value.refresh_token);
                        const decryptedUser = this.jwtHelper.decodeToken(value.access_token);
                        console.log(decryptedUser);
                        const data = {
                            access_token: value.access_token,
                            refresh_token: value.refresh_token,
                            username: decryptedUser.username,
                            userid: decryptedUser.sub,
                            tokenExpiration: decryptedUser.user
                        };
                        this.userInfo.next(data);
                        return true;
                    }
                    return false;
                    
                })
            );
}

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 2023-04-04
    • 2018-04-17
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2012-10-31
    相关资源
    最近更新 更多