HttpClient返回未定义的响应

时间:2019-05-13 17:56:22

标签: angular jwt httpclient response

我正在尝试使用httpclient获取我的access_token。但是,响应始终是不确定的。

      login(email:string, password:string) {
        var user = 
        {  
          "user": {
              "email": "name@test.com",
              "password": "password",
            }
        };
        return this.httpClient.post<{access_token:  string}>('http://localhost:3000/login', user).pipe(tap(res => {
        localStorage.setItem('access_token', res.access_token);
    }))
    }

edit:这是我的应用程序组件:

export class AppComponent {
  title = 'app works!';
  constructor(private authToken: JwtService){

    this.authToken.login("user11@example.com", "password").subscribe(

        res => {

          console.log('auth response:', res);
        },

        err => {
          console.error('auth error:', err);
        }
    )
  }
}

1 个答案:

答案 0 :(得分:0)

从此更改代码

      var user = 
            {  
              "user": {
                  "email": "name@test.com",
                  "password": "password",
                }
            };
return this.httpClient.post<{access_token:  string}>('http://localhost:3000/login', user)

对此

  let user = 
  {  
    "email": "name@test.com",
    "password": "password"
  };

  return this.httpClient.post('http://localhost:3000/login', user).pipe(tap(res => {
        localStorage.setItem('access_token', res.access_token);

并尝试使用console.log(res)来查看您的代码中是否有任何响应