简体   繁体   English

我如何将一个从http.post返回的简单字符串值设置为Angular5中的变量

[英]How can i set a simple string value returned from http.post to a variable in Angular5

I want to take a token from my Restful webservice with http.post. 我想使用http.post从我的Restful Web服务中获取令牌。 The webservice takes an username and password, if parameters match with any user in database it creates a token and return it as a string. Web服务使用用户名和密码,如果参数与数据库中的任何用户匹配,它将创建一个令牌并将其作为字符串返回。 I can send post and take the value as JSON but i want to take it as string. 我可以发送帖子,并将值作为JSON,但我想将其作为字符串。 and i wanna use it as variable. 我想将其用作变量。 And i want to have clear knowledge about http and rest service comunication if anyone can suggest a source it would be very nice to me. 我想对http和rest service通讯有清楚的了解,如果有人可以建议一个来源对我来说很好。 Thanks in advance... 提前致谢...

i can take it with this but i wanna take it as a variable or a variable inside a class Interface : 我可以接受它,但我想将其作为一个变量或一个类Interface中的变量:

export interface Token {
    token: string
}

auth.service.ts : auth.service.ts:

login (username: string, password: string): Observable<Token[]> {
      this.params = '?username=' + username + '&password=' + password;
      return this.http.post<Token[]>(this.loginURL + this.params, null, httpOptions).do(data => console.log(data));
  }

login.component.ts : login.component.ts:

this.authService.login(this.username, this.password).subscribe(data => this.authService.token = data[0]);

UPDATED AND WORKING 更新和工作

I solved it with @SrAxi's help. 我在@SrAxi的帮助下解决了。 The Final correct code; 最终的正确代码;

my login.component.ts : 我的login.component.ts:

this.authService.login(this.username, this.password).subscribe(
            (data: string) => this.authService.token.token = data,
            (error: any) => console.error(error.message, error.details),() => console.log(this.authService.token.token)
        );

My auth.service.ts : 我的auth.service.ts:

 login(username: string, password: string): Observable<string> {
        this.params = '?username=' + username + '&password=' + password;
        return this.http.post(this.loginURL + this.params, null, httpOptions)
            .map((response: Token) => response.token) // we navigate in the repsonse directly and fish out the token and return it
            .catch((error: any) => Observable.throw(error));
    }

and my object class : 和我的对象类:

export class Token {
    private _token: string;


    get token(): string {
        return this._token;
    }

    set token(value: string) {
        this._token = value;
    }
}

You are not mapping the response from your Http call. 您没有映射来自Http调用的响应。

login(username: string, password: string): Observable<string> {
    this.params = '?username=' + username + '&password=' + password;
    return this.http.post(this.loginURL + this.params, null, httpOptions)
      .map((response: any) => response.json().token) // we navigate in the repsonse directly and fish out the token and return it
      .catch((error: any) => Observable.throw(error));
  }

With this reworked function you'll be able to map the response and parse it as a JSON or to throw an error that your subscription will catch. 使用此功能,您将能够映射响应并将其解析为JSON或引发订阅将捕获的错误。

So you could call it like this: 因此,您可以这样称呼它:

this.authService.login(this.username, this.password).subscribe(
      (data: any) => this.authService.token = data[0],
      (error: any) => console.error(error.message, error.details)
    );

In your auth-service do the following 在您的auth-service中执行以下操作

login (username: string, password: string): Observable<Token> {
  this.params = '?username=' + username + '&password=' + password;
  return this.http.post(this.loginURL + this.params, null, httpOptions).map((res: Token) => res);
  }

Then in your login-component 然后在您的登录组件中

    ...
    token: Token = null;
    this.authService.login(this.username, this.password)
   .subscribe(token => this.token = token);
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM