发布请求帮助angular2

时间:2017-10-23 18:35:58

标签: javascript angular post

我正在尝试发布帖子请求以获取一个令我可以访问访问令牌的令牌。每当我尝试发布它时,我都会收到一个错误,即access_token属性无法读取未定义的内容。我很确定这意味着我的帖子请求不起作用,我不知道为什么。我认为这与我的帖子参数以及地图和订阅方法有关。我只是不知道如何解决它们。任何帮助将不胜感激,谢谢。

这是我的帖子请求代码。

httpPostRequest() {
    this.grabState();
    this.grabCode();
    this.grabSessionStorage();
    this.grabTokenUri();
    this.grabClientId();
    this.grabSecret();
    this.grabServiceUri();
    this.grabRedirectUri();
    this.data = {
      code: this.code,
      grant_type: 'authorization_code',
      redirect_uri: this.redirectUri,
    };
    const headers = new Headers({'Accept': 'application/json'});
    const options = new RequestOptions({ headers: headers });
    return this._http.post(this.tokenUri, this.data, options)
      .map((postResponse: Response) => postResponse.json())
      .subscribe(resPostResponse => (this.postResponseJson = resPostResponse));
  }

1 个答案:

答案 0 :(得分:0)

Angular HttpClient异步,因此您必须订阅您的帖子请求(例如,在您的视图中)。现在它正在返回undefined,因为它尚未执行。获取令牌后,您可以使用回调来执行另一个函数。

相关问题