Angular2:想要在获得第一个响应后调用第二个API?

时间:2017-08-05 13:43:25

标签: angular angular2-routing observable

我是Angular2的新手。有人可以帮我解决下面的问题。我想打电话给' update_settings'获得Login的响应后的API作为登录响应/信息在第二个API中使用。发生的事情是我无法调用第二个API。它只是从登录功能返回而没有任何输出。登录API工作正常,但第二个没有。在Angular 1中我们使用链接但在这里我尝试了很多东西,但没有任何作用。有人可以在这方面帮助我吗?

login(username, password): Observable<any> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    return this._httpService
      .post(
        'login_URL',
        JSON.stringify( { username: username, pass: password }),
        {headers}
      )
      .map(res => {
        if (res) {
          this._httpService.post('update_settings_URL', JSON.stringify( { client_id: res.client_id }))
          .map(new_res => {
            return { token: res.access_token };
          })
        }
      });
  }

http.service.ts

post(uri: string, body: string, options?: RequestOptionsArgs): Observable<any> {
    return this._http.post(this.endPointFullUrl(uri), body, this.requestOptionsArgs(options))
      .map(response => {
        try {
          return response.json();
        } catch (err) {
          /*In case if response body is empty, 200 response code also invokes
            error handlers as empty 'response.json()' throws exception.
            This way an undefined is returned as the response object
            which can easily be handled in the success observers down the chain.
          */
        }
      });
  }

0 个答案:

没有答案