【问题标题】:Ionic2 / Angular2 MailChimp API GET responseIonic2 / Angular2 MailChimp API GET 响应
【发布时间】:2017-03-19 20:15:16
【问题描述】:

好吧,我对 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)
      );
  }

当我调用&lt;p&gt;{{ response }}&lt;/p&gt; 时,在主页 html 中写出“待定”或“已订阅”。但是在我尝试console.log(this.response); 之后,它什么也没写,所以在代码中我无法真正进行检查。

【问题讨论】:

  • 您的回复看起来如何?你的预期输出是什么?您在哪里尝试并 console.log 响应?
  • 此响应仅是来自 json 的状态数据,因此它仅显示该文本。所以它只在&lt;p&gt;{{ response }}&lt;/p&gt; 中显示“已订阅”、“待定”等。我期望/想要一个字符串(我试过 JSON.stringify),所以我可以用一个简单的 if 语句检查状态。我尝试在 .ts 文件中的所有位置注销。
  • 可能还没有理解,但是status,你的意思是像响应状态,例如200?因为现在您正尝试在回复中显示属性 status。如果你想要响应的状态,你应该只做.map(res =&gt; res)然后你可以订阅response.status
  • 嗯。我实际上在 postCheck 函数中做了 .map(res => res) 而在 sendCheck 函数中做了订阅不是吗?我试图更好地解释我需要什么。如果我在这两个函数工作时知道它是正确的,它会在响应中返回一个 JSON 文件,我想要来自该 JSON 文件的状态值,它可以是“订阅”、“待定”等。最后当我得到了所有这些数据,我只想用一个简单的 if 语句检查状态,例如:if(this.status == "subscribed"){something happen}
  • 好吧,我想我现在可能明白你想要做什么了……意思是,在你的回复到达后做一些事情。稍等,我会写一个答案。

标签: php angular ionic2 mailchimp


【解决方案1】:

据我所知,您希望在数据到达后对您的响应做一些事情。您需要在订阅中执行此操作,以确保数据可用。所以是这样的:

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
  }
}

【讨论】:

  • 有效!!非常感谢你救了我的命。我已经尝试了 2 周来解决这个问题。
  • 没问题,不客气,很高兴能帮上忙! :)
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 2015-09-28
  • 2017-02-15
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 2020-05-08
  • 2018-03-13
相关资源
最近更新 更多