Ionic2 / Angular2 MailChimp API GET 响应

Ionic2 / Angular2 MailChimp API GET response

好的,我在 MailChimp 响应方面遇到了一点问题。事情是这样的。

我想查看订阅用户的状态。我有工作正常的 PHP 代码,我有也工作正常的代码,所以我得到了响应,但我不能使用它之后的响应。所以这是代码:

我有一个包含此功能的 MailService 提供程序:

postCheck(post: {email: string}): Observable<any>{
    const email = JSON.stringify(post);
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-urlencoded');
    return this.http.post('http://localhost:8100/getapi', email, {
      headers: headers
    }).map(res => {
          console.log(res.json())
          return res.json(); //add this line
       });
  }

在主页我有这个功能:

sendCheck(email: string){
      this.mailservice.postCheck({email: email})
      .subscribe(
        response => this.response = response.status,
        error => console.log(error)
      );
  }

在主页 html 中,当我调用 <p>{{ response }}</p> 时,它会写出 'pending' 或 'subscribed'。但是在我尝试 console.log(this.response); 之后,它什么也没写,所以在代码中我无法真正进行检查。

据我所知,您想在数据到达后对您的响应做些什么。您需要在订阅内部执行此操作,以确保数据可用。所以像这样:

sendCheck(email: string){
  this.mailservice.postCheck({email: email})
  .subscribe(response => { 
     this.response = response.status;
     // here the value has been set to response, so do what you want
     this.doSomethingWithResponse();
  )};
}

doSomethingWithResponse() {
  if(this.response == 'subscribed') {
     // do stuff
  }
}