Okey所以我对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>
时,它会写出'待定'或'已订阅'。但是当我尝试console.log(this.response);
之后,它没有写出任何内容,所以在代码中我无法真正进行检查。
答案 0 :(得分: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
}
}